A program to automatically tune a guitar. Written by Justin Reidhead and Steven Swenson
Dependencies: FFT FrequencyFinder Motor NewTextLCD PinDetect mbed strings
FrequencyFinder/FrequencyFinder.cpp@11:bdad1acccdad, 2012-04-25 (annotated)
- Committer:
- melangeaddict
- Date:
- Wed Apr 25 00:42:46 2012 +0000
- Revision:
- 11:bdad1acccdad
- Parent:
- 10:4566e8aeb3ae
- Child:
- 12:03c189de6e2e
messed with gains of the strings;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
melangeaddict | 2:9c0a83c5ded5 | 1 | #include "FrequencyFinder.h" |
melangeaddict | 2:9c0a83c5ded5 | 2 | |
melangeaddict | 2:9c0a83c5ded5 | 3 | using namespace std; |
melangeaddict | 2:9c0a83c5ded5 | 4 | |
melangeaddict | 9:2a211133e79a | 5 | FrequencyFinder::FrequencyFinder(PinName input) : _signal_in(input), _dc_offset(p18) { |
melangeaddict | 2:9c0a83c5ded5 | 6 | index=0; |
melangeaddict | 2:9c0a83c5ded5 | 7 | peak=0; |
melangeaddict | 2:9c0a83c5ded5 | 8 | frequency_final=0; |
melangeaddict | 2:9c0a83c5ded5 | 9 | } |
melangeaddict | 2:9c0a83c5ded5 | 10 | |
melangeaddict | 2:9c0a83c5ded5 | 11 | FrequencyFinder::~FrequencyFinder() { |
melangeaddict | 2:9c0a83c5ded5 | 12 | ticker.detach(); |
melangeaddict | 2:9c0a83c5ded5 | 13 | } |
melangeaddict | 2:9c0a83c5ded5 | 14 | |
melangeaddict | 2:9c0a83c5ded5 | 15 | float FrequencyFinder::find_frequency() { |
melangeaddict | 2:9c0a83c5ded5 | 16 | initialize_array(); |
melangeaddict | 2:9c0a83c5ded5 | 17 | ticker.attach_us(this, &FrequencyFinder::get_data,500); |
melangeaddict | 2:9c0a83c5ded5 | 18 | |
melangeaddict | 2:9c0a83c5ded5 | 19 | wait(1); |
melangeaddict | 2:9c0a83c5ded5 | 20 | ticker.detach(); |
melangeaddict | 2:9c0a83c5ded5 | 21 | |
melangeaddict | 9:2a211133e79a | 22 | copy_data(); |
melangeaddict | 2:9c0a83c5ded5 | 23 | |
melangeaddict | 10:4566e8aeb3ae | 24 | vRealFFT(signal_array,size); |
melangeaddict | 10:4566e8aeb3ae | 25 | |
melangeaddict | 2:9c0a83c5ded5 | 26 | take_abs(); |
melangeaddict | 2:9c0a83c5ded5 | 27 | |
melangeaddict | 2:9c0a83c5ded5 | 28 | int peak = find_peak(); |
melangeaddict | 2:9c0a83c5ded5 | 29 | |
melangeaddict | 2:9c0a83c5ded5 | 30 | float frequency_final=((float)peak)/4.1; |
melangeaddict | 2:9c0a83c5ded5 | 31 | |
melangeaddict | 2:9c0a83c5ded5 | 32 | return frequency_final; |
melangeaddict | 2:9c0a83c5ded5 | 33 | } |
melangeaddict | 2:9c0a83c5ded5 | 34 | |
melangeaddict | 2:9c0a83c5ded5 | 35 | void FrequencyFinder::get_data() { |
melangeaddict | 2:9c0a83c5ded5 | 36 | if (index<size) { |
melangeaddict | 10:4566e8aeb3ae | 37 | signal_array[index]=_signal_in.read();//-0.5; |
melangeaddict | 2:9c0a83c5ded5 | 38 | index++; |
melangeaddict | 2:9c0a83c5ded5 | 39 | } |
melangeaddict | 2:9c0a83c5ded5 | 40 | } |
melangeaddict | 2:9c0a83c5ded5 | 41 | |
melangeaddict | 2:9c0a83c5ded5 | 42 | void FrequencyFinder::take_abs() { |
melangeaddict | 2:9c0a83c5ded5 | 43 | //takes the absolute value of the signal |
melangeaddict | 2:9c0a83c5ded5 | 44 | for (int j=0; j<size; j++) { |
melangeaddict | 2:9c0a83c5ded5 | 45 | signal_array[j]=abs(signal_array[j]); |
melangeaddict | 11:bdad1acccdad | 46 | if (j<200)//~48Hz |
melangeaddict | 2:9c0a83c5ded5 | 47 | signal_array[j]=0; |
melangeaddict | 11:bdad1acccdad | 48 | else if (j<400)//~97Hz |
melangeaddict | 8:651fbf5ae98a | 49 | signal_array[j]=signal_array[j]*5; |
melangeaddict | 11:bdad1acccdad | 50 | else if (j<600)//~146Hz |
melangeaddict | 8:651fbf5ae98a | 51 | signal_array[j]=signal_array[j]*6; |
melangeaddict | 11:bdad1acccdad | 52 | else if (j<900)//~219Hz |
melangeaddict | 8:651fbf5ae98a | 53 | signal_array[j]=signal_array[j]*7; |
melangeaddict | 11:bdad1acccdad | 54 | else if (j<1200)//292Hz |
melangeaddict | 11:bdad1acccdad | 55 | signal_array[j]=signal_array[j]*25; |
melangeaddict | 2:9c0a83c5ded5 | 56 | else |
melangeaddict | 11:bdad1acccdad | 57 | signal_array[j]=signal_array[j]*30; |
melangeaddict | 2:9c0a83c5ded5 | 58 | } |
melangeaddict | 2:9c0a83c5ded5 | 59 | } |
melangeaddict | 2:9c0a83c5ded5 | 60 | |
melangeaddict | 2:9c0a83c5ded5 | 61 | int FrequencyFinder::find_peak() { |
melangeaddict | 2:9c0a83c5ded5 | 62 | int i=0; |
melangeaddict | 2:9c0a83c5ded5 | 63 | |
melangeaddict | 2:9c0a83c5ded5 | 64 | for (int j=0; j<size; j++) { |
melangeaddict | 2:9c0a83c5ded5 | 65 | if ((signal_array[i]-signal_array[i-1])>100 ) { |
melangeaddict | 2:9c0a83c5ded5 | 66 | while ( |
melangeaddict | 2:9c0a83c5ded5 | 67 | (signal_array[i]<=signal_array[i+1]) && |
melangeaddict | 2:9c0a83c5ded5 | 68 | (signal_array[i]<=signal_array[i+2]) && |
melangeaddict | 2:9c0a83c5ded5 | 69 | (signal_array[i]<=signal_array[i+3]) && |
melangeaddict | 2:9c0a83c5ded5 | 70 | (signal_array[i]<=signal_array[i+4])) { |
melangeaddict | 2:9c0a83c5ded5 | 71 | i++; |
melangeaddict | 2:9c0a83c5ded5 | 72 | } |
melangeaddict | 2:9c0a83c5ded5 | 73 | return FrequencyFinder::find_max(i); |
melangeaddict | 2:9c0a83c5ded5 | 74 | } |
melangeaddict | 2:9c0a83c5ded5 | 75 | i++; |
melangeaddict | 2:9c0a83c5ded5 | 76 | } |
melangeaddict | 2:9c0a83c5ded5 | 77 | return FrequencyFinder::find_max(i); |
melangeaddict | 2:9c0a83c5ded5 | 78 | } |
melangeaddict | 2:9c0a83c5ded5 | 79 | |
melangeaddict | 2:9c0a83c5ded5 | 80 | int FrequencyFinder::find_max(int position) { |
melangeaddict | 2:9c0a83c5ded5 | 81 | int index1=position-20; |
melangeaddict | 2:9c0a83c5ded5 | 82 | int index2=position+20; |
melangeaddict | 2:9c0a83c5ded5 | 83 | float max=signal_array[index1]; |
melangeaddict | 2:9c0a83c5ded5 | 84 | int max_index=0; |
melangeaddict | 2:9c0a83c5ded5 | 85 | |
melangeaddict | 2:9c0a83c5ded5 | 86 | for (int a=index1; a<index2; a++) { |
melangeaddict | 2:9c0a83c5ded5 | 87 | if (signal_array[a]>max) { |
melangeaddict | 2:9c0a83c5ded5 | 88 | max=signal_array[a]; |
melangeaddict | 2:9c0a83c5ded5 | 89 | max_index=a; |
melangeaddict | 2:9c0a83c5ded5 | 90 | } |
melangeaddict | 2:9c0a83c5ded5 | 91 | } |
melangeaddict | 2:9c0a83c5ded5 | 92 | |
melangeaddict | 2:9c0a83c5ded5 | 93 | return max_index; |
melangeaddict | 2:9c0a83c5ded5 | 94 | } |
melangeaddict | 2:9c0a83c5ded5 | 95 | |
melangeaddict | 2:9c0a83c5ded5 | 96 | void FrequencyFinder::initialize_array() { |
melangeaddict | 2:9c0a83c5ded5 | 97 | index=0; |
melangeaddict | 2:9c0a83c5ded5 | 98 | for (int w=0; w<size; w++) { |
melangeaddict | 2:9c0a83c5ded5 | 99 | signal_array[w]=0; |
melangeaddict | 2:9c0a83c5ded5 | 100 | } |
melangeaddict | 9:2a211133e79a | 101 | } |
melangeaddict | 9:2a211133e79a | 102 | |
melangeaddict | 9:2a211133e79a | 103 | void FrequencyFinder::copy_data() { |
melangeaddict | 9:2a211133e79a | 104 | for (int i=0; i<size/2; i++) { |
melangeaddict | 9:2a211133e79a | 105 | signal_array[i+(size/2)]=signal_array[i]; |
melangeaddict | 9:2a211133e79a | 106 | } |
melangeaddict | 2:9c0a83c5ded5 | 107 | } |