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