Craig Evans
/
AudioIn
Reads in audio signal on ADC, performs FFT and displays spectrum on Nokia 5110 LCD display.
main.cpp@1:7c6c1f98b8f5, 2014-07-22 (annotated)
- Committer:
- eencae
- Date:
- Tue Jul 22 15:42:39 2014 +0000
- Revision:
- 1:7c6c1f98b8f5
- Parent:
- 0:a497562bb2f9
- Child:
- 2:8da22b498051
Added in spectrum analyser display on Nokia 5110 LCD.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 0:a497562bb2f9 | 1 | #include "mbed.h" |
eencae | 1:7c6c1f98b8f5 | 2 | #include "N5110.h" |
eencae | 0:a497562bb2f9 | 3 | |
eencae | 0:a497562bb2f9 | 4 | extern "C" void fftR4(short *y, short *x, int N); |
eencae | 0:a497562bb2f9 | 5 | |
eencae | 0:a497562bb2f9 | 6 | float magnitude(short y1, short y2); |
eencae | 0:a497562bb2f9 | 7 | void updateSamples(); |
eencae | 0:a497562bb2f9 | 8 | void doFFT(); |
eencae | 0:a497562bb2f9 | 9 | void printSpectrum(); |
eencae | 0:a497562bb2f9 | 10 | void printSamples(); |
eencae | 1:7c6c1f98b8f5 | 11 | void ledBarGraph(); |
eencae | 1:7c6c1f98b8f5 | 12 | void lcdEqualiser(); |
eencae | 0:a497562bb2f9 | 13 | int calcPeakFrequency(); |
eencae | 0:a497562bb2f9 | 14 | |
eencae | 1:7c6c1f98b8f5 | 15 | AnalogIn audio(p17); // ADC pin must be biased at Vcc/2 using coupling capacitor and potential divider |
eencae | 0:a497562bb2f9 | 16 | BusOut leds(LED1,LED2,LED3,LED4); |
eencae | 0:a497562bb2f9 | 17 | LocalFileSystem local("local"); // Create the local filesystem under the name "local" |
eencae | 0:a497562bb2f9 | 18 | Serial serial(USBTX,USBRX); |
eencae | 1:7c6c1f98b8f5 | 19 | // VCC,SCE,RST,D/C,MOSI,SCLK,LED |
eencae | 1:7c6c1f98b8f5 | 20 | N5110 lcd(p7,p8,p9,p10,p11,p13,p21); |
eencae | 0:a497562bb2f9 | 21 | |
eencae | 0:a497562bb2f9 | 22 | #define BUF_LEN 1024 |
eencae | 0:a497562bb2f9 | 23 | #define SAMP_FREQ 10000 |
eencae | 0:a497562bb2f9 | 24 | |
eencae | 1:7c6c1f98b8f5 | 25 | short samples[BUF_LEN]; // store the values read from ADC |
eencae | 0:a497562bb2f9 | 26 | short mx[BUF_LEN*2]; // input data 16 bit, 4 byte aligned x0r,x0i,x1r,x1i,.... |
eencae | 0:a497562bb2f9 | 27 | short my[BUF_LEN*2]; // output data 16 bit,4 byte aligned y0r,y0i,y1r,y1i,.... |
eencae | 1:7c6c1f98b8f5 | 28 | float spectrum[BUF_LEN/2]; // frequency spectrum |
eencae | 1:7c6c1f98b8f5 | 29 | |
eencae | 1:7c6c1f98b8f5 | 30 | char buffer[14]; // screen buffer |
eencae | 0:a497562bb2f9 | 31 | |
eencae | 0:a497562bb2f9 | 32 | int main() |
eencae | 0:a497562bb2f9 | 33 | { |
eencae | 1:7c6c1f98b8f5 | 34 | // initialise LCD and display welcomes message |
eencae | 1:7c6c1f98b8f5 | 35 | lcd.init(); |
eencae | 1:7c6c1f98b8f5 | 36 | lcd.printString("Audio FFT",14,0); |
eencae | 1:7c6c1f98b8f5 | 37 | lcd.printString("Analyser",20,1); |
eencae | 1:7c6c1f98b8f5 | 38 | lcd.printString("Craig A. Evans",0,4); |
eencae | 0:a497562bb2f9 | 39 | serial.baud(115200); |
eencae | 0:a497562bb2f9 | 40 | leds = 15; |
eencae | 1:7c6c1f98b8f5 | 41 | wait(2.0); // short pause to allow coupling capacitor to charge |
eencae | 0:a497562bb2f9 | 42 | leds = 0; |
eencae | 1:7c6c1f98b8f5 | 43 | lcd.clear(); |
eencae | 0:a497562bb2f9 | 44 | |
eencae | 0:a497562bb2f9 | 45 | while(1) { |
eencae | 0:a497562bb2f9 | 46 | |
eencae | 1:7c6c1f98b8f5 | 47 | updateSamples(); // read in new analog values |
eencae | 1:7c6c1f98b8f5 | 48 | ledBarGraph(); // display amplitude bar graph on LEDs from sample values |
eencae | 1:7c6c1f98b8f5 | 49 | doFFT(); // calc FFT |
eencae | 1:7c6c1f98b8f5 | 50 | lcdEqualiser(); // plot spectrum on LCD |
eencae | 1:7c6c1f98b8f5 | 51 | |
eencae | 0:a497562bb2f9 | 52 | //printSpectrum(); |
eencae | 0:a497562bb2f9 | 53 | //printSamples(); |
eencae | 1:7c6c1f98b8f5 | 54 | //int tone = calcPeakFrequency(); // calculate peak frequcny and send over serial for debug |
eencae | 1:7c6c1f98b8f5 | 55 | //serial.printf("f = %u\n",tone); |
eencae | 1:7c6c1f98b8f5 | 56 | |
eencae | 1:7c6c1f98b8f5 | 57 | wait_ms(100); // update display 100 ms |
eencae | 0:a497562bb2f9 | 58 | |
eencae | 0:a497562bb2f9 | 59 | } |
eencae | 0:a497562bb2f9 | 60 | } |
eencae | 0:a497562bb2f9 | 61 | |
eencae | 1:7c6c1f98b8f5 | 62 | void ledBarGraph() |
eencae | 0:a497562bb2f9 | 63 | { |
eencae | 1:7c6c1f98b8f5 | 64 | float rms = 0.0; // initialse array |
eencae | 0:a497562bb2f9 | 65 | for (int i = 0; i < BUF_LEN; i++) { |
eencae | 0:a497562bb2f9 | 66 | rms+= samples[i]*samples[i]; |
eencae | 0:a497562bb2f9 | 67 | } |
eencae | 1:7c6c1f98b8f5 | 68 | // calc the sum of the squares |
eencae | 1:7c6c1f98b8f5 | 69 | |
eencae | 1:7c6c1f98b8f5 | 70 | rms/=BUF_LEN; // get the mean |
eencae | 1:7c6c1f98b8f5 | 71 | rms = sqrt(rms); // and root to get the RMS |
eencae | 0:a497562bb2f9 | 72 | rms/= 16384.0; // scale according to 16-bit signed maximum value |
eencae | 0:a497562bb2f9 | 73 | |
eencae | 1:7c6c1f98b8f5 | 74 | // check value and update LEDs to show amplitude |
eencae | 0:a497562bb2f9 | 75 | if (rms > 0.8) { |
eencae | 0:a497562bb2f9 | 76 | leds = 15; |
eencae | 0:a497562bb2f9 | 77 | } else if (rms > 0.6) { |
eencae | 0:a497562bb2f9 | 78 | leds = 7; |
eencae | 0:a497562bb2f9 | 79 | } else if (rms > 0.4) { |
eencae | 0:a497562bb2f9 | 80 | leds = 3; |
eencae | 0:a497562bb2f9 | 81 | } else if (rms > 0.2) { |
eencae | 0:a497562bb2f9 | 82 | leds = 1; |
eencae | 0:a497562bb2f9 | 83 | } else { |
eencae | 0:a497562bb2f9 | 84 | leds = 0; |
eencae | 0:a497562bb2f9 | 85 | } |
eencae | 0:a497562bb2f9 | 86 | |
eencae | 1:7c6c1f98b8f5 | 87 | //serial.printf("RMS = %f\n",rms); |
eencae | 0:a497562bb2f9 | 88 | } |
eencae | 0:a497562bb2f9 | 89 | |
eencae | 0:a497562bb2f9 | 90 | float magnitude(short y1, short y2) |
eencae | 0:a497562bb2f9 | 91 | { |
eencae | 1:7c6c1f98b8f5 | 92 | return sqrt(float(y1*y1+y2*y2)); // pythagoras |
eencae | 0:a497562bb2f9 | 93 | } |
eencae | 0:a497562bb2f9 | 94 | |
eencae | 0:a497562bb2f9 | 95 | void updateSamples() |
eencae | 0:a497562bb2f9 | 96 | { |
eencae | 0:a497562bb2f9 | 97 | for (int i = 0; i < BUF_LEN; i++) { |
eencae | 0:a497562bb2f9 | 98 | samples[i] = (short) (audio.read_u16() - 0x8000); // read unsigned 16-bit and convert to signed 16-bit (subtract 32768) |
eencae | 1:7c6c1f98b8f5 | 99 | wait_us(1e6/SAMP_FREQ); // wait for sampling frequency, should really implement with tickers |
eencae | 0:a497562bb2f9 | 100 | } |
eencae | 0:a497562bb2f9 | 101 | } |
eencae | 0:a497562bb2f9 | 102 | |
eencae | 0:a497562bb2f9 | 103 | void doFFT() |
eencae | 0:a497562bb2f9 | 104 | { |
eencae | 1:7c6c1f98b8f5 | 105 | // clear buffers |
eencae | 0:a497562bb2f9 | 106 | for (int i=0; i<2*BUF_LEN; i++) { |
eencae | 0:a497562bb2f9 | 107 | my[i] = 0; |
eencae | 0:a497562bb2f9 | 108 | mx[i] = 0; |
eencae | 0:a497562bb2f9 | 109 | } |
eencae | 0:a497562bb2f9 | 110 | |
eencae | 1:7c6c1f98b8f5 | 111 | for (int i=0; i<BUF_LEN; i++) { // load samples in array (skip imaginary input values) |
eencae | 0:a497562bb2f9 | 112 | mx[i*2]=samples[i]; |
eencae | 0:a497562bb2f9 | 113 | } |
eencae | 0:a497562bb2f9 | 114 | |
eencae | 0:a497562bb2f9 | 115 | fftR4(my, mx, BUF_LEN); // call FFT routine |
eencae | 0:a497562bb2f9 | 116 | |
eencae | 0:a497562bb2f9 | 117 | int j = 0; |
eencae | 0:a497562bb2f9 | 118 | for (int i = 0; i < BUF_LEN; i+=2) { |
eencae | 1:7c6c1f98b8f5 | 119 | spectrum[j] = magnitude(my[i],my[i+1]); // get magnitude of FFT output to get spectrum data |
eencae | 0:a497562bb2f9 | 120 | j++; |
eencae | 0:a497562bb2f9 | 121 | } |
eencae | 0:a497562bb2f9 | 122 | } |
eencae | 0:a497562bb2f9 | 123 | |
eencae | 0:a497562bb2f9 | 124 | void printSpectrum() |
eencae | 0:a497562bb2f9 | 125 | { |
eencae | 0:a497562bb2f9 | 126 | FILE *fp = fopen("/local/fft.csv","w"); |
eencae | 0:a497562bb2f9 | 127 | //now write a CSV file to filesytem of frequency vs amplitude |
eencae | 0:a497562bb2f9 | 128 | int j = 0; |
eencae | 0:a497562bb2f9 | 129 | for (int i = 0; i < BUF_LEN; i+=2) { |
eencae | 1:7c6c1f98b8f5 | 130 | int frequency = int(SAMP_FREQ/BUF_LEN/2*i); // calculate value of frequency bin |
eencae | 0:a497562bb2f9 | 131 | fprintf(fp, "%d,%f\n", frequency, spectrum[j]); |
eencae | 0:a497562bb2f9 | 132 | j++; |
eencae | 0:a497562bb2f9 | 133 | } |
eencae | 0:a497562bb2f9 | 134 | fclose(fp); |
eencae | 0:a497562bb2f9 | 135 | } |
eencae | 0:a497562bb2f9 | 136 | |
eencae | 0:a497562bb2f9 | 137 | void printSamples() |
eencae | 0:a497562bb2f9 | 138 | { |
eencae | 0:a497562bb2f9 | 139 | FILE *fp = fopen("/local/samples.csv","w"); |
eencae | 0:a497562bb2f9 | 140 | //now write a CSV file to filesytem of frequency vs amplitude |
eencae | 0:a497562bb2f9 | 141 | for (int i = 0; i < BUF_LEN; i++) { |
eencae | 0:a497562bb2f9 | 142 | fprintf(fp, "%d\n", samples[i]); |
eencae | 0:a497562bb2f9 | 143 | } |
eencae | 0:a497562bb2f9 | 144 | fclose(fp); |
eencae | 0:a497562bb2f9 | 145 | } |
eencae | 0:a497562bb2f9 | 146 | |
eencae | 0:a497562bb2f9 | 147 | int calcPeakFrequency() |
eencae | 0:a497562bb2f9 | 148 | { |
eencae | 0:a497562bb2f9 | 149 | float max = 0.0; |
eencae | 0:a497562bb2f9 | 150 | int frequency = 0; |
eencae | 0:a497562bb2f9 | 151 | int j = 0; |
eencae | 1:7c6c1f98b8f5 | 152 | for (int i=0; i<BUF_LEN; i+=2) { // loop through spectrum and look for maximum value |
eencae | 0:a497562bb2f9 | 153 | if (spectrum[j] > max) { |
eencae | 0:a497562bb2f9 | 154 | max = spectrum[j]; |
eencae | 1:7c6c1f98b8f5 | 155 | frequency = int(SAMP_FREQ/BUF_LEN/2*i); |
eencae | 0:a497562bb2f9 | 156 | } |
eencae | 0:a497562bb2f9 | 157 | j++; |
eencae | 0:a497562bb2f9 | 158 | } |
eencae | 1:7c6c1f98b8f5 | 159 | //serial.printf("Max = %f\n",max); |
eencae | 0:a497562bb2f9 | 160 | return frequency; |
eencae | 1:7c6c1f98b8f5 | 161 | } |
eencae | 1:7c6c1f98b8f5 | 162 | |
eencae | 1:7c6c1f98b8f5 | 163 | void lcdEqualiser() |
eencae | 1:7c6c1f98b8f5 | 164 | { |
eencae | 1:7c6c1f98b8f5 | 165 | // spectrum has BUF_LEN/2 values = 512 |
eencae | 1:7c6c1f98b8f5 | 166 | // screen has 84 pixel, giving 6 spectrum points per pixel |
eencae | 1:7c6c1f98b8f5 | 167 | |
eencae | 1:7c6c1f98b8f5 | 168 | float max = 0.0; // used for normalisation later |
eencae | 1:7c6c1f98b8f5 | 169 | |
eencae | 1:7c6c1f98b8f5 | 170 | float pixelValue[84]; |
eencae | 1:7c6c1f98b8f5 | 171 | |
eencae | 1:7c6c1f98b8f5 | 172 | for (int i=0; i<84; i++) { // loop through array |
eencae | 1:7c6c1f98b8f5 | 173 | pixelValue[i] = 0.0; |
eencae | 1:7c6c1f98b8f5 | 174 | |
eencae | 1:7c6c1f98b8f5 | 175 | for (int y=0; y<6; y++) { |
eencae | 1:7c6c1f98b8f5 | 176 | pixelValue[i] += spectrum[i*6+y]; // sum the 6 values in the spectrum |
eencae | 1:7c6c1f98b8f5 | 177 | } |
eencae | 1:7c6c1f98b8f5 | 178 | pixelValue[i]/=6; // calc. average for that pixel |
eencae | 1:7c6c1f98b8f5 | 179 | |
eencae | 1:7c6c1f98b8f5 | 180 | if (pixelValue[i] > max) // check for biggest value |
eencae | 1:7c6c1f98b8f5 | 181 | max = pixelValue[i]; |
eencae | 1:7c6c1f98b8f5 | 182 | } |
eencae | 1:7c6c1f98b8f5 | 183 | |
eencae | 1:7c6c1f98b8f5 | 184 | for (int i=0; i<84; i++) { // loop through array |
eencae | 1:7c6c1f98b8f5 | 185 | pixelValue[i]/=max; // normalise to 1.0 |
eencae | 1:7c6c1f98b8f5 | 186 | } |
eencae | 1:7c6c1f98b8f5 | 187 | |
eencae | 1:7c6c1f98b8f5 | 188 | lcd.clear(); |
eencae | 1:7c6c1f98b8f5 | 189 | for (int i=0; i<84; i++) { // loop through array |
eencae | 1:7c6c1f98b8f5 | 190 | for (int j=0; j<= 47 - int(pixelValue[i]*47.0); j++) { // loop through array |
eencae | 1:7c6c1f98b8f5 | 191 | lcd.setPixel(i,j); // draw bar graphs for spectrum bins |
eencae | 1:7c6c1f98b8f5 | 192 | } |
eencae | 1:7c6c1f98b8f5 | 193 | } |
eencae | 1:7c6c1f98b8f5 | 194 | lcd.refresh(); |
eencae | 1:7c6c1f98b8f5 | 195 | |
eencae | 0:a497562bb2f9 | 196 | } |