void do_fft_test() { int i, scale; unsigned diff; short x[N], fx[N]; printf("Samples: \n"); for (i=0; i<N; i++) { /* x[i] = AMPLITUDE*cos(i*FREQUENCY*(2*3.1415926535)/N); if (i & 0x01) fx[(N+i)>>1] = x[i]; else fx[i>>1] = x[i]; */ #if DEBUG show_amp(i,x[i], AMPLITUDE); #endif } fix_fftr(fx, log2N, 0); printf("----------------------------------------------\n"); printf("- Frequencies -\n"); printf("----------------------------------------------\n"); for (i=0; i<N/2; i++) show_amp(i, fx[i], AMPLITUDE); #if SPECTRUM for (i=0; i<N/2; i++) show_amp(i, fx[i], AMPLITUDE); return; #endif printf("----------------------------------------------\n"); printf("- Scaled -\n"); printf("----------------------------------------------\n"); scale = fix_fftr(fx, log2N, 1); //fprintf(stderr, "scale = %d\n", scale); for (i=0,diff=0; i<N; i++) { int sample; if (i & 0x01) sample = fx[(N+i)>>1] << scale; else sample = fx[i>>1] << scale; #if DEBUG show_amp(i, sample, AMPLITUDE); #endif diff += abs(x[i]-sample); }
int main(int argc, char* argv[]) { volatile int16_t note = -1; Output_init(); ADC_init(); // Initialize the ADC Timer2_init(); // Initialize Tim2 while (1) { if(index == max_index){//we've finished the song... start over! index = 0; } // Check if a data array is ready for FFT processing if(arrayComplete==1){ arrayComplete = 0; // Reset arrayComplete // Process the array that completed! if(array1Complete == 1){ int16_t out = fix_fftr(dataArray1,10,0); note = getNote(dataArray1); array1Complete = 0;//mark array as available to re-fill } else if(array2Complete == 1) { int16_t out = fix_fftr(dataArray2,10,0); note = getNote(dataArray2); array2Complete = 0;//mark array as available to re-fill } int16_t selectedNote = processNote(note); switch(selectedNote) { case -2: // Pause (actually play a frequency so high that no one but dogs can hear it) TIM2->ARR = 1000000/40000; // Set frequency (40 kHz) break; case -1: // Continue playing the same note break; case 0: // Generate C4 262 Hz TIM2->ARR = 1000000/262/4; // Set frequency break; case 1: // Generate C#4 277 Hz TIM2->ARR = 1000000/277/4; // Set frequency break; case 2: // Generate D4 294 Hz TIM2->ARR = 1000000/294/4; // Set frequency break; case 3: // Generate D#4 311 Hz TIM2->ARR = 1000000/311/4; // Set frequency break; case 4: // Generate E4 330 Hz TIM2->ARR = 1000000/330/4; // Set frequency break; case 5: // Generate F4 349 Hz TIM2->ARR = 1000000/349/4; // Set frequency break; case 6: // Generate F#4 370 Hz TIM2->ARR = 1000000/370/4; // Set frequency break; case 7: // Generate G4 392 Hz TIM2->ARR = 1000000/392/4; // Set frequency break; case 8: // Generate G#4 415 Hz TIM2->ARR = 1000000/415/4; // Set frequency break; case 9: // Generate A4 440 Hz TIM2->ARR = 1000000/440/4; // Set frequency break; case 10: // Generate A# 466 Hz TIM2->ARR = 1000000/466/4; // Set frequency break; case 11: // Generate B4 494 Hz TIM2->ARR = 1000000/494/4; // Set frequency break; case 12: // Generate C5 523 Hz TIM2->ARR = 1000000/523/4; // Set frequency break; case 13: // Generate C#6 554 Hz TIM2->ARR = 1000000/554/4; // Set frequency break; default: break; } } } }