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@8:651fbf5ae98a, 2012-04-20 (annotated)
- Committer:
- melangeaddict
- Date:
- Fri Apr 20 21:27:55 2012 +0000
- Revision:
- 8:651fbf5ae98a
- Parent:
- 2:9c0a83c5ded5
- Child:
- 9:2a211133e79a
3:30 april 20;
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 | 2:9c0a83c5ded5 | 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 | 2:9c0a83c5ded5 | 22 | vRealFFT(signal_array-1,size); |
melangeaddict | 2:9c0a83c5ded5 | 23 | |
melangeaddict | 2:9c0a83c5ded5 | 24 | take_abs(); |
melangeaddict | 2:9c0a83c5ded5 | 25 | |
melangeaddict | 2:9c0a83c5ded5 | 26 | int peak = find_peak(); |
melangeaddict | 2:9c0a83c5ded5 | 27 | |
melangeaddict | 2:9c0a83c5ded5 | 28 | float frequency_final=((float)peak)/4.1; |
melangeaddict | 2:9c0a83c5ded5 | 29 | |
melangeaddict | 2:9c0a83c5ded5 | 30 | return frequency_final; |
melangeaddict | 2:9c0a83c5ded5 | 31 | } |
melangeaddict | 2:9c0a83c5ded5 | 32 | |
melangeaddict | 2:9c0a83c5ded5 | 33 | void FrequencyFinder::get_data() { |
melangeaddict | 2:9c0a83c5ded5 | 34 | if (index<size) { |
melangeaddict | 2:9c0a83c5ded5 | 35 | signal_array[index]=_signal_in.read();//-0.5); |
melangeaddict | 2:9c0a83c5ded5 | 36 | index++; |
melangeaddict | 2:9c0a83c5ded5 | 37 | } |
melangeaddict | 2:9c0a83c5ded5 | 38 | } |
melangeaddict | 2:9c0a83c5ded5 | 39 | |
melangeaddict | 2:9c0a83c5ded5 | 40 | void FrequencyFinder::take_abs() { |
melangeaddict | 2:9c0a83c5ded5 | 41 | //takes the absolute value of the signal |
melangeaddict | 2:9c0a83c5ded5 | 42 | for (int j=0; j<size; j++) { |
melangeaddict | 2:9c0a83c5ded5 | 43 | signal_array[j]=abs(signal_array[j]); |
melangeaddict | 2:9c0a83c5ded5 | 44 | if (j<100) |
melangeaddict | 2:9c0a83c5ded5 | 45 | signal_array[j]=0; |
melangeaddict | 2:9c0a83c5ded5 | 46 | else if (j<400) |
melangeaddict | 8:651fbf5ae98a | 47 | signal_array[j]=signal_array[j]*5; |
melangeaddict | 2:9c0a83c5ded5 | 48 | else if (j<600) |
melangeaddict | 8:651fbf5ae98a | 49 | signal_array[j]=signal_array[j]*6; |
melangeaddict | 2:9c0a83c5ded5 | 50 | else if (j<800) |
melangeaddict | 8:651fbf5ae98a | 51 | signal_array[j]=signal_array[j]*7; |
melangeaddict | 2:9c0a83c5ded5 | 52 | else |
melangeaddict | 8:651fbf5ae98a | 53 | signal_array[j]=signal_array[j]*8; |
melangeaddict | 2:9c0a83c5ded5 | 54 | } |
melangeaddict | 2:9c0a83c5ded5 | 55 | } |
melangeaddict | 2:9c0a83c5ded5 | 56 | |
melangeaddict | 2:9c0a83c5ded5 | 57 | int FrequencyFinder::find_peak() { |
melangeaddict | 2:9c0a83c5ded5 | 58 | int i=0; |
melangeaddict | 2:9c0a83c5ded5 | 59 | |
melangeaddict | 2:9c0a83c5ded5 | 60 | for (int j=0; j<size; j++) { |
melangeaddict | 2:9c0a83c5ded5 | 61 | if ((signal_array[i]-signal_array[i-1])>100 ) { |
melangeaddict | 2:9c0a83c5ded5 | 62 | while ( |
melangeaddict | 2:9c0a83c5ded5 | 63 | (signal_array[i]<=signal_array[i+1]) && |
melangeaddict | 2:9c0a83c5ded5 | 64 | (signal_array[i]<=signal_array[i+2]) && |
melangeaddict | 2:9c0a83c5ded5 | 65 | (signal_array[i]<=signal_array[i+3]) && |
melangeaddict | 2:9c0a83c5ded5 | 66 | (signal_array[i]<=signal_array[i+4])) { |
melangeaddict | 2:9c0a83c5ded5 | 67 | i++; |
melangeaddict | 2:9c0a83c5ded5 | 68 | } |
melangeaddict | 2:9c0a83c5ded5 | 69 | return FrequencyFinder::find_max(i); |
melangeaddict | 2:9c0a83c5ded5 | 70 | } |
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 | |
melangeaddict | 2:9c0a83c5ded5 | 76 | int FrequencyFinder::find_max(int position) { |
melangeaddict | 2:9c0a83c5ded5 | 77 | int index1=position-20; |
melangeaddict | 2:9c0a83c5ded5 | 78 | int index2=position+20; |
melangeaddict | 2:9c0a83c5ded5 | 79 | float max=signal_array[index1]; |
melangeaddict | 2:9c0a83c5ded5 | 80 | int max_index=0; |
melangeaddict | 2:9c0a83c5ded5 | 81 | |
melangeaddict | 2:9c0a83c5ded5 | 82 | for (int a=index1; a<index2; a++) { |
melangeaddict | 2:9c0a83c5ded5 | 83 | if (signal_array[a]>max) { |
melangeaddict | 2:9c0a83c5ded5 | 84 | max=signal_array[a]; |
melangeaddict | 2:9c0a83c5ded5 | 85 | max_index=a; |
melangeaddict | 2:9c0a83c5ded5 | 86 | } |
melangeaddict | 2:9c0a83c5ded5 | 87 | } |
melangeaddict | 2:9c0a83c5ded5 | 88 | |
melangeaddict | 2:9c0a83c5ded5 | 89 | return max_index; |
melangeaddict | 2:9c0a83c5ded5 | 90 | } |
melangeaddict | 2:9c0a83c5ded5 | 91 | |
melangeaddict | 2:9c0a83c5ded5 | 92 | void FrequencyFinder::initialize_array() { |
melangeaddict | 2:9c0a83c5ded5 | 93 | index=0; |
melangeaddict | 2:9c0a83c5ded5 | 94 | for (int w=0; w<size; w++) { |
melangeaddict | 2:9c0a83c5ded5 | 95 | signal_array[w]=0; |
melangeaddict | 2:9c0a83c5ded5 | 96 | } |
melangeaddict | 2:9c0a83c5ded5 | 97 | } |