Change the Ledstrip with Different input signal frequency
Dependencies: PololuLedStrip mbed
main.cpp
00001 #define SAMPLE_RATE 48000 00002 00003 00004 #include "mbed.h" 00005 #include "adc.h" 00006 #include "PololuLedStrip.h" 00007 00008 PololuLedStrip ledStrip(p8); 00009 00010 #define LED_COUNT 60 00011 rgb_color colors[LED_COUNT]; 00012 00013 Serial pc(USBTX,USBRX); 00014 00015 //extern "C" void cr4_fft_256_stm32(void *pssOUT, void *pssIN, uint16_t Nbin); 00016 extern "C" void fftR4(short *y, short *x, int N); 00017 00018 //use the LED as a bargraph 00019 DigitalOut l1(LED1); 00020 DigitalOut l2(LED2); 00021 DigitalOut l3(LED3); 00022 DigitalOut l4(LED4); 00023 00024 //set up a timer for timing FFT's 00025 Timer timer; 00026 00027 //Used to change colour of LED strips 00028 //int testX = 0; 00029 00030 //Set up filesystem so we can write some useful files 00031 00032 LocalFileSystem local("local"); 00033 FILE *fp; 00034 00035 //Set up a global buffer for audio data so interrupt can access it 00036 int Counter = 0; 00037 int16_t Buffer[5000]; 00038 int16_t VoltageBuffer[5000]; 00039 00040 //Initialise ADC to maximum SAMPLE_RATE and cclk divide set to 1 00041 ADC adc(SAMPLE_RATE, 1); 00042 00043 int colourVoltage=0; 00044 00045 00046 00047 //Our interrupt handler for audio sampling 00048 void sample_ADC(int chan, uint32_t value) { 00049 00050 float s; 00051 s = adc.read(p20); 00052 int16_t b = (s -2048)*16; 00053 Buffer[Counter] = b; 00054 00055 Counter += 1; 00056 /* bar graph */ 00057 00058 int g = abs(s-2048); 00059 l1 = g > 0.1f*2048; 00060 l2 = g > 0.3f*2048; 00061 l3 = g > 0.6f*2048; 00062 l4 = g > 0.8f*2048; 00063 } 00064 00065 00066 00067 00068 00069 00070 int main() { 00071 00072 while (1){ 00073 00074 //Prepare for burst mode on all ADC pins and set up interrupt handler (using ADC library from Simon Blandford 00075 adc.append(sample_ADC); 00076 adc.startmode(0,0); 00077 adc.burst(1); 00078 adc.setup(p20,1); 00079 //introduce a delay as initial waveform has bias whilst decoupling cap charges 00080 wait(0.05); 00081 //start the interrupt and wait for about 4096 samples 00082 adc.interrupt_state(p20,1); 00083 wait(0.05); 00084 00085 //Finsh up - Unset pin 20 00086 adc.interrupt_state(p20,0); 00087 adc.setup(p20,0); 00088 int actual_rate = adc.actual_sample_rate(); 00089 //now lets try mellen fft------------------------- 00090 timer.reset(); 00091 timer.start(); 00092 #define MN 1024 /*Number of points*/ 00093 short mx[MN*2]; // input data 16 bit, 4 byte aligned x0r,x0i,x1r,x1i,.... 00094 short my[MN*2]; // output data 16 bit,4 byte aligned y0r,y0i,y1r,y1i,.... 00095 float mz[MN*2]; // to store the final result 00096 for (int i=0;i<MN*2;i++) mx[i]=0; 00097 for (int i=0;i<MN*2;i++) mz[i]=0; 00098 for (int i=0;i<MN;i=i+1) 00099 { mx[i*2]=Buffer[i];} 00100 //printf("Mellen set up took %i\n",timer.read_us()); 00101 //call functions 00102 timer.reset(); 00103 timer.start(); 00104 fftR4(my, mx, MN); 00105 //printf("Mellen fft took %i\n",timer.read_us()); 00106 //FILE* mlog = fopen("/local/mellen.csv","w"); 00107 00108 //now write a CSV file to filesytem of frequency vs amplitude 00109 for (int i=0; i<MN; i=i+2) 00110 { 00111 // fprintf(mlog, "%d: %d -> %d\n", i, mx[i], my[i]); 00112 //fprintf(mlog, "%d,%f\n", int(actual_rate/MN/2*i),sqrt(float( (my[i]*my[i]) +(my[i+1]*my[i+1]) ) ) ); 00113 00114 mz[i]=sqrt(float( (my[i]*my[i]) +(my[i+1]*my[i+1]) ) ) ; 00115 //fprintf(mlog, "%f\n", mz[i] ); 00116 } 00117 00118 00119 // detect the change of input frequency ****************************** 00120 float maxFreq=0; 00121 float max=0; 00122 for (int i=2;i<512;i=i+1){ 00123 00124 if(mz[i]>max){ 00125 max = mz[i]; 00126 maxFreq=i; 00127 }//end if 00128 00129 }//end for 00130 //fprintf(mlog, "%d\n",maxFreq ); 00131 00132 int maxVoltage=0; 00133 for (int i=0;i<512;i=i+1){ 00134 00135 if(mx[i]>maxVoltage){ 00136 maxVoltage = mx[i]; 00137 00138 }//end if 00139 }//end for 00140 00141 pc.printf(" the max = %f\n",max ); 00142 pc.printf(" the maxFreq = %f\n",maxFreq ); 00143 pc.printf(" the colourValtage = %d\n", maxVoltage ); 00144 00145 00146 /*for (int i = 0; i < LED_COUNT; i++){ 00147 colors[i] = (rgb_color){ 0, 0, 0 }; 00148 } 00149 00150 if (maxFreq>60) maxFreq=60; // Limiting Length 00151 00152 for (int i = 0; i < maxFreq; i++){ 00153 colors[i] = (rgb_color){ abs(maxVoltage/11),abs(255-(maxVoltage/11)) ,abs(255-(maxVoltage/11)+50) }; 00154 } 00155 ledStrip.write(colors, LED_COUNT);*/ 00156 int actualFreq= (actual_rate/MN/2*maxFreq); 00157 pc.printf(" actualFreq = %d\n", actualFreq ); 00158 00159 00160 //wait_ms(10); 00161 00162 /*if (maxFreq>30){ 00163 //testX=200; 00164 for (int i = 0; i < LED_COUNT; i++){ 00165 colors[i] = (rgb_color){ 0, 20, 200 }; 00166 } 00167 } 00168 00169 if ((maxFreq>22)&&(maxFreq<30)){// if frequency bigger than 966Hz 00170 //testX= 10; 00171 for (int i = 0; i < LED_COUNT; i++){ 00172 colors[i] = (rgb_color){ 250, 0, 0 }; 00173 00174 } 00175 } 00176 if (maxFreq<22){ 00177 //testX=200; 00178 for (int i = 0; i < LED_COUNT; i++){ 00179 colors[i] = (rgb_color){ 0, 250, 0 }; 00180 } 00181 }*/ 00182 00183 00184 00185 00186 00187 00188 00189 //----------- 00190 00191 00192 //fclose(mlog); 00193 Counter = 0; 00194 }// end while (1) 00195 00196 00197 }
Generated on Wed Jul 13 2022 13:30:42 by 1.7.2