Jared's DAC Code
Dependencies: mbed
Fork of Chemical_Sensor_DMA by
SignalProcessing.cpp@6:63de50ac29be, 2015-11-06 (annotated)
- Committer:
- DeWayneDennis
- Date:
- Fri Nov 06 20:50:30 2015 +0000
- Revision:
- 6:63de50ac29be
- Parent:
- 5:1b2dc43e8947
- Child:
- 7:af255a90505e
Jared's DAC Code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
baxterja | 3:a85b742be262 | 1 | #include "mbed.h" |
baxterja | 3:a85b742be262 | 2 | #include "SignalProcessing.h" |
baxterja | 2:3771b3195c7b | 3 | |
baxterja | 3:a85b742be262 | 4 | #define pre_compute_length 2000 |
baxterja | 2:3771b3195c7b | 5 | #define DMA_PERIOD .00001 |
baxterja | 2:3771b3195c7b | 6 | #define DMA_FREQUENCY 100000 |
baxterja | 2:3771b3195c7b | 7 | #define CARRIERFREQUENCY 10000 |
baxterja | 3:a85b742be262 | 8 | |
baxterja | 3:a85b742be262 | 9 | uint16_t phase_counter = 0; |
baxterja | 3:a85b742be262 | 10 | |
baxterja | 2:3771b3195c7b | 11 | float i_mod_pre[pre_compute_length]; |
baxterja | 2:3771b3195c7b | 12 | float q_mod_pre[pre_compute_length]; |
baxterja | 4:9fd291254686 | 13 | //uint16_t out_val_pre[pre_compute_length]; |
DeWayneDennis | 6:63de50ac29be | 14 | float filteredLong, filteredLongRef; |
baxterja | 2:3771b3195c7b | 15 | |
baxterja | 4:9fd291254686 | 16 | #define twopi 3.14159265359 * 2 |
baxterja | 2:3771b3195c7b | 17 | |
baxterja | 2:3771b3195c7b | 18 | void pre_compute_tables() { |
baxterja | 3:a85b742be262 | 19 | |
baxterja | 2:3771b3195c7b | 20 | // This function will precompute the cos and sin tables used in the rest of the program |
baxterja | 2:3771b3195c7b | 21 | for(int precompute_counter = 0; precompute_counter < pre_compute_length; precompute_counter++){ |
baxterja | 4:9fd291254686 | 22 | //out_val_pre[precompute_counter] = (int) (cos(twopi * CARRIERFREQUENCY * DMA_PERIOD * precompute_counter) * 4965.0 + 49650.0); |
baxterja | 3:a85b742be262 | 23 | i_mod_pre[precompute_counter] = (cos(twopi * CARRIERFREQUENCY * DMA_PERIOD * precompute_counter)); |
baxterja | 3:a85b742be262 | 24 | q_mod_pre[precompute_counter] = (-sin(twopi * CARRIERFREQUENCY * DMA_PERIOD * precompute_counter)); |
baxterja | 2:3771b3195c7b | 25 | |
baxterja | 2:3771b3195c7b | 26 | } |
baxterja | 3:a85b742be262 | 27 | } |
baxterja | 3:a85b742be262 | 28 | |
baxterja | 3:a85b742be262 | 29 | |
baxterja | 3:a85b742be262 | 30 | |
baxterja | 4:9fd291254686 | 31 | /* |
baxterja | 4:9fd291254686 | 32 | #define FIR_33_LENGTH 128 |
baxterja | 4:9fd291254686 | 33 | float FIR33_Sample1_i[FIR_33_LENGTH]; |
baxterja | 4:9fd291254686 | 34 | float FIR33_Sample1_q[FIR_33_LENGTH]; |
baxterja | 4:9fd291254686 | 35 | float FIR33_Sample2_i[FIR_33_LENGTH]; |
baxterja | 4:9fd291254686 | 36 | float FIR33_Sample2_q[FIR_33_LENGTH]; |
baxterja | 4:9fd291254686 | 37 | uint8_t FIR33_Position = 0; |
baxterja | 4:9fd291254686 | 38 | //Fs = 33, Order = 63, Fpass = 1, Fstop = 5 |
baxterja | 4:9fd291254686 | 39 | static float lp_33_coeff[FIR_33_LENGTH] = { |
baxterja | 4:9fd291254686 | 40 | 0.00112064914891618, |
baxterja | 4:9fd291254686 | 41 | 0.00122265940667415, |
baxterja | 4:9fd291254686 | 42 | 0.00134320266606308, |
baxterja | 4:9fd291254686 | 43 | 0.00148248497505270, |
baxterja | 4:9fd291254686 | 44 | 0.00163752208873233, |
baxterja | 4:9fd291254686 | 45 | 0.00180917436717814, |
baxterja | 4:9fd291254686 | 46 | 0.00199487440500326, |
baxterja | 4:9fd291254686 | 47 | 0.00219591677377202, |
baxterja | 4:9fd291254686 | 48 | 0.00240942435359224, |
baxterja | 4:9fd291254686 | 49 | 0.00263715016478107, |
baxterja | 4:9fd291254686 | 50 | 0.00287620469595143, |
baxterja | 4:9fd291254686 | 51 | 0.00312896642110311, |
baxterja | 4:9fd291254686 | 52 | 0.00339115146671554, |
baxterja | 4:9fd291254686 | 53 | 0.00366703367280862, |
baxterja | 4:9fd291254686 | 54 | 0.00395013103364271, |
baxterja | 4:9fd291254686 | 55 | 0.00424959421024652, |
baxterja | 4:9fd291254686 | 56 | 0.00454749937640992, |
baxterja | 4:9fd291254686 | 57 | 0.00486587065943205, |
baxterja | 4:9fd291254686 | 58 | 0.00519015600428925, |
baxterja | 4:9fd291254686 | 59 | 0.00551644394778870, |
baxterja | 4:9fd291254686 | 60 | 0.00585208754960567, |
baxterja | 4:9fd291254686 | 61 | 0.00619545680874284, |
baxterja | 4:9fd291254686 | 62 | 0.00654599064376495, |
baxterja | 4:9fd291254686 | 63 | 0.00690046871786865, |
baxterja | 4:9fd291254686 | 64 | 0.00725861046415282, |
baxterja | 4:9fd291254686 | 65 | 0.00761931288503866, |
baxterja | 4:9fd291254686 | 66 | 0.00798229719629724, |
baxterja | 4:9fd291254686 | 67 | 0.00834687323478277, |
baxterja | 4:9fd291254686 | 68 | 0.00871239637379653, |
baxterja | 4:9fd291254686 | 69 | 0.00907848882392502, |
baxterja | 4:9fd291254686 | 70 | 0.00944349817409369, |
baxterja | 4:9fd291254686 | 71 | 0.00980681579778287, |
baxterja | 4:9fd291254686 | 72 | 0.0101666025128362, |
baxterja | 4:9fd291254686 | 73 | 0.0105235376400116, |
baxterja | 4:9fd291254686 | 74 | 0.0108740956379341, |
baxterja | 4:9fd291254686 | 75 | 0.0112200337619850, |
baxterja | 4:9fd291254686 | 76 | 0.0115576132398033, |
baxterja | 4:9fd291254686 | 77 | 0.0118892792678013, |
baxterja | 4:9fd291254686 | 78 | 0.0122101053096016, |
baxterja | 4:9fd291254686 | 79 | 0.0125220143316710, |
baxterja | 4:9fd291254686 | 80 | 0.0128250887436567, |
baxterja | 4:9fd291254686 | 81 | 0.0131145746752160, |
baxterja | 4:9fd291254686 | 82 | 0.0133925589239844, |
baxterja | 4:9fd291254686 | 83 | 0.0136577821847874, |
baxterja | 4:9fd291254686 | 84 | 0.0139097056313474, |
baxterja | 4:9fd291254686 | 85 | 0.0141461938484364, |
baxterja | 4:9fd291254686 | 86 | 0.0143672111207676, |
baxterja | 4:9fd291254686 | 87 | 0.0145723586994284, |
baxterja | 4:9fd291254686 | 88 | 0.0147612534972782, |
baxterja | 4:9fd291254686 | 89 | 0.0149330287990815, |
baxterja | 4:9fd291254686 | 90 | 0.0150868196634048, |
baxterja | 4:9fd291254686 | 91 | 0.0152228115376150, |
baxterja | 4:9fd291254686 | 92 | 0.0153400576626228, |
baxterja | 4:9fd291254686 | 93 | 0.0154384553803622, |
baxterja | 4:9fd291254686 | 94 | 0.0155165567546008, |
baxterja | 4:9fd291254686 | 95 | 0.0155766985839343, |
baxterja | 4:9fd291254686 | 96 | 0.0156161625679816, |
baxterja | 4:9fd291254686 | 97 | 0.0156360898266519, |
baxterja | 4:9fd291254686 | 98 | 0.0156360898266519, |
baxterja | 4:9fd291254686 | 99 | 0.0156161625679816, |
baxterja | 4:9fd291254686 | 100 | 0.0155766985839343, |
baxterja | 4:9fd291254686 | 101 | 0.0155165567546008, |
baxterja | 4:9fd291254686 | 102 | 0.0154384553803622, |
baxterja | 4:9fd291254686 | 103 | 0.0153400576626228, |
baxterja | 4:9fd291254686 | 104 | 0.0152228115376150, |
baxterja | 4:9fd291254686 | 105 | 0.0150868196634048, |
baxterja | 4:9fd291254686 | 106 | 0.0149330287990815, |
baxterja | 4:9fd291254686 | 107 | 0.0147612534972782, |
baxterja | 4:9fd291254686 | 108 | 0.0145723586994284, |
baxterja | 4:9fd291254686 | 109 | 0.0143672111207676, |
baxterja | 4:9fd291254686 | 110 | 0.0141461938484364, |
baxterja | 4:9fd291254686 | 111 | 0.0139097056313474, |
baxterja | 4:9fd291254686 | 112 | 0.0136577821847874, |
baxterja | 4:9fd291254686 | 113 | 0.0133925589239844, |
baxterja | 4:9fd291254686 | 114 | 0.0131145746752160, |
baxterja | 4:9fd291254686 | 115 | 0.0128250887436567, |
baxterja | 4:9fd291254686 | 116 | 0.0125220143316710, |
baxterja | 4:9fd291254686 | 117 | 0.0122101053096016, |
baxterja | 4:9fd291254686 | 118 | 0.0118892792678013, |
baxterja | 4:9fd291254686 | 119 | 0.0115576132398033, |
baxterja | 4:9fd291254686 | 120 | 0.0112200337619850, |
baxterja | 4:9fd291254686 | 121 | 0.0108740956379341, |
baxterja | 4:9fd291254686 | 122 | 0.0105235376400116, |
baxterja | 4:9fd291254686 | 123 | 0.0101666025128362, |
baxterja | 4:9fd291254686 | 124 | 0.00980681579778287, |
baxterja | 4:9fd291254686 | 125 | 0.00944349817409369, |
baxterja | 4:9fd291254686 | 126 | 0.00907848882392502, |
baxterja | 4:9fd291254686 | 127 | 0.00871239637379653, |
baxterja | 4:9fd291254686 | 128 | 0.00834687323478277, |
baxterja | 4:9fd291254686 | 129 | 0.00798229719629724, |
baxterja | 4:9fd291254686 | 130 | 0.00761931288503866, |
baxterja | 4:9fd291254686 | 131 | 0.00725861046415282, |
baxterja | 4:9fd291254686 | 132 | 0.00690046871786865, |
baxterja | 4:9fd291254686 | 133 | 0.00654599064376495, |
baxterja | 4:9fd291254686 | 134 | 0.00619545680874284, |
baxterja | 4:9fd291254686 | 135 | 0.00585208754960567, |
baxterja | 4:9fd291254686 | 136 | 0.00551644394778870, |
baxterja | 4:9fd291254686 | 137 | 0.00519015600428925, |
baxterja | 4:9fd291254686 | 138 | 0.00486587065943205, |
baxterja | 4:9fd291254686 | 139 | 0.00454749937640992, |
baxterja | 4:9fd291254686 | 140 | 0.00424959421024652, |
baxterja | 4:9fd291254686 | 141 | 0.00395013103364271, |
baxterja | 4:9fd291254686 | 142 | 0.00366703367280862, |
baxterja | 4:9fd291254686 | 143 | 0.00339115146671554, |
baxterja | 4:9fd291254686 | 144 | 0.00312896642110311, |
baxterja | 4:9fd291254686 | 145 | 0.00287620469595143, |
baxterja | 4:9fd291254686 | 146 | 0.00263715016478107, |
baxterja | 4:9fd291254686 | 147 | 0.00240942435359224, |
baxterja | 4:9fd291254686 | 148 | 0.00219591677377202, |
baxterja | 4:9fd291254686 | 149 | 0.00199487440500326, |
baxterja | 4:9fd291254686 | 150 | 0.00180917436717814, |
baxterja | 4:9fd291254686 | 151 | 0.00163752208873233, |
baxterja | 4:9fd291254686 | 152 | 0.00148248497505270, |
baxterja | 4:9fd291254686 | 153 | 0.00134320266606308, |
baxterja | 4:9fd291254686 | 154 | 0.00122265940667415, |
baxterja | 4:9fd291254686 | 155 | 0.00112064914891618, |
baxterja | 4:9fd291254686 | 156 | 0.00104032814636243, |
baxterja | 4:9fd291254686 | 157 | 0.000982028128151230, |
baxterja | 4:9fd291254686 | 158 | 0.000950473399969370, |
baxterja | 4:9fd291254686 | 159 | 0.000946711675637481, |
baxterja | 4:9fd291254686 | 160 | 0.000976800083269572, |
baxterja | 4:9fd291254686 | 161 | -0.00572883284452795 |
baxterja | 4:9fd291254686 | 162 | }; |
baxterja | 4:9fd291254686 | 163 | #define NUMSAMPLESAVERAGE 33 |
baxterja | 4:9fd291254686 | 164 | #define DecimationFactor_33 3 |
baxterja | 4:9fd291254686 | 165 | |
baxterja | 4:9fd291254686 | 166 | void filter33(float FIR33_Sample1_i_input, float FIR33_Sample1_q_input, float FIR33_Sample2_i_input, float FIR33_Sample2_q_input) |
baxterja | 4:9fd291254686 | 167 | { |
baxterja | 4:9fd291254686 | 168 | //printf("f"); |
baxterja | 4:9fd291254686 | 169 | static uint8_t finalAverageCounter = 0; |
baxterja | 4:9fd291254686 | 170 | static uint8_t decimationCounter = 0;//used to keep track of how many samples you have currently decimated |
baxterja | 4:9fd291254686 | 171 | static float FIR33_Sample1_i_DecimatedSum=0; |
baxterja | 4:9fd291254686 | 172 | static float FIR33_Sample1_q_DecimatedSum=0;//when decimating sum up all 10 samples at a time have that be your output value |
baxterja | 4:9fd291254686 | 173 | static float FIR33_Sample2_i_DecimatedSum=0; |
baxterja | 4:9fd291254686 | 174 | static float FIR33_Sample2_q_DecimatedSum=0; |
baxterja | 4:9fd291254686 | 175 | |
baxterja | 4:9fd291254686 | 176 | static float Final_Average1_i=0; |
baxterja | 4:9fd291254686 | 177 | static float Final_Average1_q=0;//when decimating sum up all 10 samples at a time have that be your output value |
baxterja | 4:9fd291254686 | 178 | static float Final_Average2_i=0; |
baxterja | 4:9fd291254686 | 179 | static float Final_Average2_q=0; |
baxterja | 4:9fd291254686 | 180 | |
baxterja | 4:9fd291254686 | 181 | FIR33_Sample1_i_DecimatedSum += FIR33_Sample1_i_input;//add sample to the sum of previous sample |
baxterja | 4:9fd291254686 | 182 | FIR33_Sample1_q_DecimatedSum += FIR33_Sample1_q_input; |
baxterja | 4:9fd291254686 | 183 | FIR33_Sample2_i_DecimatedSum += FIR33_Sample2_i_input; |
baxterja | 4:9fd291254686 | 184 | FIR33_Sample2_q_DecimatedSum += FIR33_Sample2_q_input; |
baxterja | 4:9fd291254686 | 185 | decimationCounter++; |
baxterja | 4:9fd291254686 | 186 | if (decimationCounter >= DecimationFactor_33)//once 10 samples have com |
baxterja | 4:9fd291254686 | 187 | { |
baxterja | 4:9fd291254686 | 188 | decimationCounter = 0;//reset decimation counter |
baxterja | 4:9fd291254686 | 189 | //add sample to 10K filter |
baxterja | 4:9fd291254686 | 190 | FIR33_Sample1_i[FIR33_Position] = FIR33_Sample1_i_DecimatedSum; |
baxterja | 4:9fd291254686 | 191 | FIR33_Sample1_q[FIR33_Position] = FIR33_Sample1_q_DecimatedSum; |
baxterja | 4:9fd291254686 | 192 | FIR33_Sample2_i[FIR33_Position] = FIR33_Sample2_i_DecimatedSum; |
baxterja | 4:9fd291254686 | 193 | FIR33_Sample2_q[FIR33_Position] = FIR33_Sample2_q_DecimatedSum; |
baxterja | 4:9fd291254686 | 194 | |
baxterja | 4:9fd291254686 | 195 | FIR33_Sample1_i_DecimatedSum = 0;//reset decimated sum to 0 for next sample |
baxterja | 4:9fd291254686 | 196 | FIR33_Sample1_q_DecimatedSum = 0; |
baxterja | 4:9fd291254686 | 197 | FIR33_Sample2_i_DecimatedSum = 0; |
baxterja | 4:9fd291254686 | 198 | FIR33_Sample2_q_DecimatedSum = 0; |
baxterja | 4:9fd291254686 | 199 | |
baxterja | 4:9fd291254686 | 200 | FIR33_Position++; //increment circular buffer |
baxterja | 4:9fd291254686 | 201 | if (FIR33_Position >= FIR_33_LENGTH) //wrap around |
baxterja | 4:9fd291254686 | 202 | { |
baxterja | 4:9fd291254686 | 203 | FIR33_Position = 0; |
baxterja | 4:9fd291254686 | 204 | } |
baxterja | 4:9fd291254686 | 205 | |
baxterja | 4:9fd291254686 | 206 | // Low pass filter of demodulated signal |
baxterja | 4:9fd291254686 | 207 | float FIR33_Sample1_i_Output = FIR33_Sample1_i[FIR33_Position] * lp_33_coeff[0];//first multiply of convolution |
baxterja | 4:9fd291254686 | 208 | float FIR33_Sample1_q_Output = FIR33_Sample1_q[FIR33_Position] * lp_33_coeff[0]; |
baxterja | 4:9fd291254686 | 209 | float FIR33_Sample2_i_Output = FIR33_Sample2_i[FIR33_Position] * lp_33_coeff[0]; |
baxterja | 4:9fd291254686 | 210 | float FIR33_Sample2_q_Output = FIR33_Sample2_q[FIR33_Position] * lp_33_coeff[0]; |
baxterja | 4:9fd291254686 | 211 | int fir_index; |
baxterja | 4:9fd291254686 | 212 | for(int fir_counter = 1; fir_counter < FIR_33_LENGTH; fir_counter++)//the rest of the convolution |
baxterja | 4:9fd291254686 | 213 | { |
baxterja | 4:9fd291254686 | 214 | fir_index = FIR33_Position + fir_counter; |
baxterja | 4:9fd291254686 | 215 | if (fir_index >= FIR_33_LENGTH) |
baxterja | 4:9fd291254686 | 216 | { |
baxterja | 4:9fd291254686 | 217 | fir_index -= FIR_33_LENGTH; |
baxterja | 4:9fd291254686 | 218 | } |
baxterja | 4:9fd291254686 | 219 | FIR33_Sample1_i_Output += FIR33_Sample1_i[fir_index] * lp_33_coeff[fir_counter];//convolving |
baxterja | 4:9fd291254686 | 220 | FIR33_Sample1_q_Output += FIR33_Sample1_q[fir_index] * lp_33_coeff[fir_counter]; |
baxterja | 4:9fd291254686 | 221 | FIR33_Sample2_i_Output += FIR33_Sample2_i[fir_index] * lp_33_coeff[fir_counter]; |
baxterja | 4:9fd291254686 | 222 | FIR33_Sample2_q_Output += FIR33_Sample2_q[fir_index] * lp_33_coeff[fir_counter]; |
baxterja | 4:9fd291254686 | 223 | } |
baxterja | 4:9fd291254686 | 224 | |
baxterja | 4:9fd291254686 | 225 | //float mag1 = sqrt(FIR33_Sample1_i_Output*FIR33_Sample1_i_Output+FIR33_Sample1_q_Output*FIR33_Sample1_q_Output); |
baxterja | 4:9fd291254686 | 226 | //float mag2 = sqrt(FIR33_Sample2_i_Output*FIR33_Sample2_i_Output+FIR33_Sample2_q_Output*FIR33_Sample2_q_Output); |
baxterja | 4:9fd291254686 | 227 | //printf("V1: %f\tV2: %f\n\r",mag1,mag2); |
baxterja | 4:9fd291254686 | 228 | |
baxterja | 4:9fd291254686 | 229 | |
baxterja | 4:9fd291254686 | 230 | Final_Average1_i+=FIR33_Sample1_i_Output; |
baxterja | 4:9fd291254686 | 231 | Final_Average1_q+=FIR33_Sample1_q_Output;//when decimating sum up all 10 samples at a time have that be your output value |
baxterja | 4:9fd291254686 | 232 | Final_Average2_i+=FIR33_Sample2_i_Output; |
baxterja | 4:9fd291254686 | 233 | Final_Average2_q+=FIR33_Sample2_q_Output; |
baxterja | 4:9fd291254686 | 234 | finalAverageCounter++; |
baxterja | 4:9fd291254686 | 235 | if (finalAverageCounter>=NUMSAMPLESAVERAGE) |
baxterja | 4:9fd291254686 | 236 | { |
baxterja | 4:9fd291254686 | 237 | finalAverageCounter=0; |
baxterja | 4:9fd291254686 | 238 | float mag1 = sqrt(Final_Average1_i*Final_Average1_i+Final_Average1_q*Final_Average1_q); |
baxterja | 4:9fd291254686 | 239 | float mag2 = sqrt(Final_Average2_i*Final_Average2_i+Final_Average2_q*Final_Average2_q); |
baxterja | 4:9fd291254686 | 240 | printf("V1: %f\tV2: %f\tRatio: %f\n\r",mag1,mag2,mag2/mag1); |
baxterja | 4:9fd291254686 | 241 | Final_Average1_i=0; |
baxterja | 4:9fd291254686 | 242 | Final_Average1_q=0;//when decimating sum up all 10 samples at a time have that be your output value |
baxterja | 4:9fd291254686 | 243 | Final_Average2_i=0; |
baxterja | 4:9fd291254686 | 244 | Final_Average2_q=0; |
baxterja | 4:9fd291254686 | 245 | } |
baxterja | 4:9fd291254686 | 246 | //float mag1 = sqrt(FIR33_Sample1_i_Output*FIR33_Sample1_i_Output+FIR33_Sample1_q_Output*FIR33_Sample1_q_Output); |
baxterja | 4:9fd291254686 | 247 | //float mag2 = sqrt(FIR33_Sample2_i_Output*FIR33_Sample2_i_Output+FIR33_Sample2_q_Output*FIR33_Sample2_q_Output); |
baxterja | 4:9fd291254686 | 248 | //printf("V1: %f\tV2: %f\n\r",mag1,mag2); |
baxterja | 4:9fd291254686 | 249 | //filter33(FIR33_Sample1_i_Output, FIR33_Sample1_q_Output, FIR33_Sample2_i_Output, FIR33_Sample2_q_Output); |
baxterja | 4:9fd291254686 | 250 | |
baxterja | 4:9fd291254686 | 251 | } |
baxterja | 4:9fd291254686 | 252 | } |
baxterja | 4:9fd291254686 | 253 | |
baxterja | 4:9fd291254686 | 254 | */ |
baxterja | 4:9fd291254686 | 255 | |
baxterja | 4:9fd291254686 | 256 | |
baxterja | 4:9fd291254686 | 257 | |
baxterja | 5:1b2dc43e8947 | 258 | /* |
baxterja | 3:a85b742be262 | 259 | #define FIR_100_LENGTH 64 |
baxterja | 3:a85b742be262 | 260 | float FIR100_Sample1_i[FIR_100_LENGTH]; |
baxterja | 3:a85b742be262 | 261 | float FIR100_Sample1_q[FIR_100_LENGTH]; |
baxterja | 3:a85b742be262 | 262 | float FIR100_Sample2_i[FIR_100_LENGTH]; |
baxterja | 3:a85b742be262 | 263 | float FIR100_Sample2_q[FIR_100_LENGTH]; |
baxterja | 3:a85b742be262 | 264 | uint8_t FIR100_Position = 0; |
baxterja | 3:a85b742be262 | 265 | //Fs = 100, Order = 63, Fpass = 1, Fstop = 5 |
baxterja | 3:a85b742be262 | 266 | static float lp_100_coeff[FIR_100_LENGTH] = { |
baxterja | 4:9fd291254686 | 267 | -0.0127764520494401, |
baxterja | 4:9fd291254686 | 268 | 0.000293288299851251, |
baxterja | 4:9fd291254686 | 269 | 0.000495191328182707, |
baxterja | 4:9fd291254686 | 270 | 0.000838157407497840, |
baxterja | 4:9fd291254686 | 271 | 0.00131525363039988, |
baxterja | 4:9fd291254686 | 272 | 0.00192793120776209, |
baxterja | 4:9fd291254686 | 273 | 0.00267432434442746, |
baxterja | 4:9fd291254686 | 274 | 0.00355353493845821, |
baxterja | 4:9fd291254686 | 275 | 0.00456036887752939, |
baxterja | 4:9fd291254686 | 276 | 0.00569272050003285, |
baxterja | 4:9fd291254686 | 277 | 0.00694090445590618, |
baxterja | 4:9fd291254686 | 278 | 0.00830083540850880, |
baxterja | 4:9fd291254686 | 279 | 0.00975882121858377, |
baxterja | 4:9fd291254686 | 280 | 0.0113079414406056, |
baxterja | 4:9fd291254686 | 281 | 0.0129307225563250, |
baxterja | 4:9fd291254686 | 282 | 0.0146171626466673, |
baxterja | 4:9fd291254686 | 283 | 0.0163450416131430, |
baxterja | 4:9fd291254686 | 284 | 0.0181032221508212, |
baxterja | 4:9fd291254686 | 285 | 0.0198618118533429, |
baxterja | 4:9fd291254686 | 286 | 0.0216165682984801, |
baxterja | 4:9fd291254686 | 287 | 0.0233125759526720, |
baxterja | 4:9fd291254686 | 288 | 0.0249849393161916, |
baxterja | 4:9fd291254686 | 289 | 0.0265739031713570, |
baxterja | 4:9fd291254686 | 290 | 0.0280649931338029, |
baxterja | 4:9fd291254686 | 291 | 0.0294518285092336, |
baxterja | 4:9fd291254686 | 292 | 0.0307008224951110, |
baxterja | 4:9fd291254686 | 293 | 0.0318087914746566, |
baxterja | 4:9fd291254686 | 294 | 0.0327529952249549, |
baxterja | 4:9fd291254686 | 295 | 0.0335263522468559, |
baxterja | 4:9fd291254686 | 296 | 0.0341150932603310, |
baxterja | 4:9fd291254686 | 297 | 0.0345128562450896, |
baxterja | 4:9fd291254686 | 298 | 0.0347130017511486, |
baxterja | 4:9fd291254686 | 299 | 0.0347130017511486, |
baxterja | 4:9fd291254686 | 300 | 0.0345128562450896, |
baxterja | 4:9fd291254686 | 301 | 0.0341150932603310, |
baxterja | 4:9fd291254686 | 302 | 0.0335263522468559, |
baxterja | 4:9fd291254686 | 303 | 0.0327529952249549, |
baxterja | 4:9fd291254686 | 304 | 0.0318087914746566, |
baxterja | 4:9fd291254686 | 305 | 0.0307008224951110, |
baxterja | 4:9fd291254686 | 306 | 0.0294518285092336, |
baxterja | 4:9fd291254686 | 307 | 0.0280649931338029, |
baxterja | 4:9fd291254686 | 308 | 0.0265739031713570, |
baxterja | 4:9fd291254686 | 309 | 0.0249849393161916, |
baxterja | 4:9fd291254686 | 310 | 0.0233125759526720, |
baxterja | 4:9fd291254686 | 311 | 0.0216165682984801, |
baxterja | 4:9fd291254686 | 312 | 0.0198618118533429, |
baxterja | 4:9fd291254686 | 313 | 0.0181032221508212, |
baxterja | 4:9fd291254686 | 314 | 0.0163450416131430, |
baxterja | 4:9fd291254686 | 315 | 0.0146171626466673, |
baxterja | 4:9fd291254686 | 316 | 0.0129307225563250, |
baxterja | 4:9fd291254686 | 317 | 0.0113079414406056, |
baxterja | 4:9fd291254686 | 318 | 0.00975882121858377, |
baxterja | 4:9fd291254686 | 319 | 0.00830083540850880, |
baxterja | 4:9fd291254686 | 320 | 0.00694090445590618, |
baxterja | 4:9fd291254686 | 321 | 0.00569272050003285, |
baxterja | 4:9fd291254686 | 322 | 0.00456036887752939, |
baxterja | 4:9fd291254686 | 323 | 0.00355353493845821, |
baxterja | 4:9fd291254686 | 324 | 0.00267432434442746, |
baxterja | 4:9fd291254686 | 325 | 0.00192793120776209, |
baxterja | 4:9fd291254686 | 326 | 0.00131525363039988, |
baxterja | 4:9fd291254686 | 327 | 0.000838157407497840, |
baxterja | 4:9fd291254686 | 328 | 0.000495191328182707, |
baxterja | 4:9fd291254686 | 329 | 0.000293288299851251, |
baxterja | 4:9fd291254686 | 330 | -0.0127764520494401 |
baxterja | 3:a85b742be262 | 331 | }; |
baxterja | 3:a85b742be262 | 332 | #define NUMSAMPLESAVERAGE 100 |
baxterja | 3:a85b742be262 | 333 | #define DecimationFactor_10K 10 |
baxterja | 3:a85b742be262 | 334 | void filter100(float FIR100_Sample1_i_input, float FIR100_Sample1_q_input, float FIR100_Sample2_i_input, float FIR100_Sample2_q_input) |
baxterja | 3:a85b742be262 | 335 | { |
baxterja | 4:9fd291254686 | 336 | //printf("f"); |
baxterja | 3:a85b742be262 | 337 | static uint8_t finalAverageCounter = 0; |
baxterja | 3:a85b742be262 | 338 | static uint8_t decimationCounter = 0;//used to keep track of how many samples you have currently decimated |
baxterja | 3:a85b742be262 | 339 | static float FIR100_Sample1_i_DecimatedSum=0; |
baxterja | 3:a85b742be262 | 340 | static float FIR100_Sample1_q_DecimatedSum=0;//when decimating sum up all 10 samples at a time have that be your output value |
baxterja | 3:a85b742be262 | 341 | static float FIR100_Sample2_i_DecimatedSum=0; |
baxterja | 3:a85b742be262 | 342 | static float FIR100_Sample2_q_DecimatedSum=0; |
baxterja | 3:a85b742be262 | 343 | |
baxterja | 3:a85b742be262 | 344 | static float Final_Average1_i=0; |
baxterja | 3:a85b742be262 | 345 | static float Final_Average1_q=0;//when decimating sum up all 10 samples at a time have that be your output value |
baxterja | 3:a85b742be262 | 346 | static float Final_Average2_i=0; |
baxterja | 3:a85b742be262 | 347 | static float Final_Average2_q=0; |
baxterja | 3:a85b742be262 | 348 | |
baxterja | 3:a85b742be262 | 349 | FIR100_Sample1_i_DecimatedSum += FIR100_Sample1_i_input;//add sample to the sum of previous sample |
baxterja | 3:a85b742be262 | 350 | FIR100_Sample1_q_DecimatedSum += FIR100_Sample1_q_input; |
baxterja | 3:a85b742be262 | 351 | FIR100_Sample2_i_DecimatedSum += FIR100_Sample2_i_input; |
baxterja | 3:a85b742be262 | 352 | FIR100_Sample2_q_DecimatedSum += FIR100_Sample2_q_input; |
baxterja | 3:a85b742be262 | 353 | decimationCounter++; |
baxterja | 3:a85b742be262 | 354 | if (decimationCounter >= DecimationFactor_10K)//once 10 samples have com |
baxterja | 3:a85b742be262 | 355 | { |
baxterja | 3:a85b742be262 | 356 | decimationCounter = 0;//reset decimation counter |
baxterja | 3:a85b742be262 | 357 | //add sample to 10K filter |
baxterja | 3:a85b742be262 | 358 | FIR100_Sample1_i[FIR100_Position] = FIR100_Sample1_i_DecimatedSum; |
baxterja | 3:a85b742be262 | 359 | FIR100_Sample1_q[FIR100_Position] = FIR100_Sample1_q_DecimatedSum; |
baxterja | 3:a85b742be262 | 360 | FIR100_Sample2_i[FIR100_Position] = FIR100_Sample2_i_DecimatedSum; |
baxterja | 3:a85b742be262 | 361 | FIR100_Sample2_q[FIR100_Position] = FIR100_Sample2_q_DecimatedSum; |
baxterja | 3:a85b742be262 | 362 | |
baxterja | 3:a85b742be262 | 363 | FIR100_Sample1_i_DecimatedSum = 0;//reset decimated sum to 0 for next sample |
baxterja | 3:a85b742be262 | 364 | FIR100_Sample1_q_DecimatedSum = 0; |
baxterja | 3:a85b742be262 | 365 | FIR100_Sample2_i_DecimatedSum = 0; |
baxterja | 3:a85b742be262 | 366 | FIR100_Sample2_q_DecimatedSum = 0; |
baxterja | 3:a85b742be262 | 367 | |
baxterja | 3:a85b742be262 | 368 | FIR100_Position++; //increment circular buffer |
baxterja | 3:a85b742be262 | 369 | if (FIR100_Position >= FIR_100_LENGTH) //wrap around |
baxterja | 3:a85b742be262 | 370 | { |
baxterja | 3:a85b742be262 | 371 | FIR100_Position = 0; |
baxterja | 3:a85b742be262 | 372 | } |
baxterja | 3:a85b742be262 | 373 | |
baxterja | 3:a85b742be262 | 374 | // Low pass filter of demodulated signal |
baxterja | 3:a85b742be262 | 375 | float FIR100_Sample1_i_Output = FIR100_Sample1_i[FIR100_Position] * lp_100_coeff[0];//first multiply of convolution |
baxterja | 3:a85b742be262 | 376 | float FIR100_Sample1_q_Output = FIR100_Sample1_q[FIR100_Position] * lp_100_coeff[0]; |
baxterja | 3:a85b742be262 | 377 | float FIR100_Sample2_i_Output = FIR100_Sample2_i[FIR100_Position] * lp_100_coeff[0]; |
baxterja | 3:a85b742be262 | 378 | float FIR100_Sample2_q_Output = FIR100_Sample2_q[FIR100_Position] * lp_100_coeff[0]; |
baxterja | 3:a85b742be262 | 379 | int fir_index; |
baxterja | 3:a85b742be262 | 380 | for(int fir_counter = 1; fir_counter < FIR_100_LENGTH; fir_counter++)//the rest of the convolution |
baxterja | 3:a85b742be262 | 381 | { |
baxterja | 3:a85b742be262 | 382 | fir_index = FIR100_Position + fir_counter; |
baxterja | 3:a85b742be262 | 383 | if (fir_index >= FIR_100_LENGTH) |
baxterja | 3:a85b742be262 | 384 | { |
baxterja | 3:a85b742be262 | 385 | fir_index -= FIR_100_LENGTH; |
baxterja | 3:a85b742be262 | 386 | } |
baxterja | 3:a85b742be262 | 387 | FIR100_Sample1_i_Output += FIR100_Sample1_i[fir_index] * lp_100_coeff[fir_counter];//convolving |
baxterja | 3:a85b742be262 | 388 | FIR100_Sample1_q_Output += FIR100_Sample1_q[fir_index] * lp_100_coeff[fir_counter]; |
baxterja | 3:a85b742be262 | 389 | FIR100_Sample2_i_Output += FIR100_Sample2_i[fir_index] * lp_100_coeff[fir_counter]; |
baxterja | 3:a85b742be262 | 390 | FIR100_Sample2_q_Output += FIR100_Sample2_q[fir_index] * lp_100_coeff[fir_counter]; |
baxterja | 3:a85b742be262 | 391 | } |
baxterja | 3:a85b742be262 | 392 | |
baxterja | 3:a85b742be262 | 393 | //float mag1 = sqrt(FIR100_Sample1_i_Output*FIR100_Sample1_i_Output+FIR100_Sample1_q_Output*FIR100_Sample1_q_Output); |
baxterja | 3:a85b742be262 | 394 | //float mag2 = sqrt(FIR100_Sample2_i_Output*FIR100_Sample2_i_Output+FIR100_Sample2_q_Output*FIR100_Sample2_q_Output); |
baxterja | 3:a85b742be262 | 395 | //printf("V1: %f\tV2: %f\n\r",mag1,mag2); |
baxterja | 3:a85b742be262 | 396 | |
baxterja | 3:a85b742be262 | 397 | |
baxterja | 3:a85b742be262 | 398 | Final_Average1_i+=FIR100_Sample1_i_Output; |
baxterja | 3:a85b742be262 | 399 | Final_Average1_q+=FIR100_Sample1_q_Output;//when decimating sum up all 10 samples at a time have that be your output value |
baxterja | 3:a85b742be262 | 400 | Final_Average2_i+=FIR100_Sample2_i_Output; |
baxterja | 3:a85b742be262 | 401 | Final_Average2_q+=FIR100_Sample2_q_Output; |
baxterja | 3:a85b742be262 | 402 | finalAverageCounter++; |
baxterja | 3:a85b742be262 | 403 | if (finalAverageCounter>=NUMSAMPLESAVERAGE) |
baxterja | 3:a85b742be262 | 404 | { |
baxterja | 3:a85b742be262 | 405 | finalAverageCounter=0; |
baxterja | 3:a85b742be262 | 406 | float mag1 = sqrt(Final_Average1_i*Final_Average1_i+Final_Average1_q*Final_Average1_q); |
baxterja | 3:a85b742be262 | 407 | float mag2 = sqrt(Final_Average2_i*Final_Average2_i+Final_Average2_q*Final_Average2_q); |
baxterja | 4:9fd291254686 | 408 | printf("V1: %f\tV2: %f\tRatio: %f\n\r",mag1,mag2,mag2/mag1); |
baxterja | 3:a85b742be262 | 409 | Final_Average1_i=0; |
baxterja | 3:a85b742be262 | 410 | Final_Average1_q=0;//when decimating sum up all 10 samples at a time have that be your output value |
baxterja | 3:a85b742be262 | 411 | Final_Average2_i=0; |
baxterja | 3:a85b742be262 | 412 | Final_Average2_q=0; |
baxterja | 3:a85b742be262 | 413 | } |
baxterja | 4:9fd291254686 | 414 | |
baxterja | 3:a85b742be262 | 415 | //float mag1 = sqrt(FIR100_Sample1_i_Output*FIR100_Sample1_i_Output+FIR100_Sample1_q_Output*FIR100_Sample1_q_Output); |
baxterja | 3:a85b742be262 | 416 | //float mag2 = sqrt(FIR100_Sample2_i_Output*FIR100_Sample2_i_Output+FIR100_Sample2_q_Output*FIR100_Sample2_q_Output); |
baxterja | 3:a85b742be262 | 417 | //printf("V1: %f\tV2: %f\n\r",mag1,mag2); |
baxterja | 4:9fd291254686 | 418 | //filter33(FIR100_Sample1_i_Output, FIR100_Sample1_q_Output, FIR100_Sample2_i_Output, FIR100_Sample2_q_Output); |
baxterja | 3:a85b742be262 | 419 | |
baxterja | 3:a85b742be262 | 420 | } |
baxterja | 3:a85b742be262 | 421 | } |
baxterja | 5:1b2dc43e8947 | 422 | */ |
baxterja | 3:a85b742be262 | 423 | |
baxterja | 3:a85b742be262 | 424 | |
baxterja | 3:a85b742be262 | 425 | |
baxterja | 3:a85b742be262 | 426 | |
baxterja | 5:1b2dc43e8947 | 427 | #define NUMSAMPLESAVERAGE 100 |
baxterja | 3:a85b742be262 | 428 | #define FIR_1K_LENGTH 32 |
baxterja | 3:a85b742be262 | 429 | float FIR1K_Sample1_i[FIR_1K_LENGTH]; |
baxterja | 3:a85b742be262 | 430 | float FIR1K_Sample1_q[FIR_1K_LENGTH]; |
baxterja | 3:a85b742be262 | 431 | float FIR1K_Sample2_i[FIR_1K_LENGTH]; |
baxterja | 3:a85b742be262 | 432 | float FIR1K_Sample2_q[FIR_1K_LENGTH]; |
baxterja | 3:a85b742be262 | 433 | uint8_t FIR1K_Position = 0; |
baxterja | 3:a85b742be262 | 434 | //Fs = 1000, Order = 31, Fpass = 1, Fstop = 50 |
baxterja | 3:a85b742be262 | 435 | static float lp_1K_coeff[FIR_1K_LENGTH] = { |
baxterja | 3:a85b742be262 | 436 | 0.0108990071119901, |
baxterja | 3:a85b742be262 | 437 | 0.00826963267129267, |
baxterja | 3:a85b742be262 | 438 | 0.0110961530344968, |
baxterja | 3:a85b742be262 | 439 | 0.0143019800886844, |
baxterja | 3:a85b742be262 | 440 | 0.0178397268153335, |
baxterja | 3:a85b742be262 | 441 | 0.0216326995075556, |
baxterja | 3:a85b742be262 | 442 | 0.0255928087296069, |
baxterja | 3:a85b742be262 | 443 | 0.0296287914936736, |
baxterja | 3:a85b742be262 | 444 | 0.0336287768230528, |
baxterja | 3:a85b742be262 | 445 | 0.0374693714591658, |
baxterja | 3:a85b742be262 | 446 | 0.0410324028349472, |
baxterja | 3:a85b742be262 | 447 | 0.0442148191339877, |
baxterja | 3:a85b742be262 | 448 | 0.0468906440777966, |
baxterja | 3:a85b742be262 | 449 | 0.0489881978583567, |
baxterja | 3:a85b742be262 | 450 | 0.0504243479483288, |
baxterja | 3:a85b742be262 | 451 | 0.0511581882807637, |
baxterja | 3:a85b742be262 | 452 | 0.0511581882807637, |
baxterja | 3:a85b742be262 | 453 | 0.0504243479483288, |
baxterja | 3:a85b742be262 | 454 | 0.0489881978583567, |
baxterja | 3:a85b742be262 | 455 | 0.0468906440777966, |
baxterja | 3:a85b742be262 | 456 | 0.0442148191339877, |
baxterja | 3:a85b742be262 | 457 | 0.0410324028349472, |
baxterja | 3:a85b742be262 | 458 | 0.0374693714591658, |
baxterja | 3:a85b742be262 | 459 | 0.0336287768230528, |
baxterja | 3:a85b742be262 | 460 | 0.0296287914936736, |
baxterja | 3:a85b742be262 | 461 | 0.0255928087296069, |
baxterja | 3:a85b742be262 | 462 | 0.0216326995075556, |
baxterja | 3:a85b742be262 | 463 | 0.0178397268153335, |
baxterja | 3:a85b742be262 | 464 | 0.0143019800886844, |
baxterja | 3:a85b742be262 | 465 | 0.0110961530344968, |
baxterja | 3:a85b742be262 | 466 | 0.00826963267129267, |
baxterja | 3:a85b742be262 | 467 | 0.0108990071119901 |
baxterja | 3:a85b742be262 | 468 | }; |
baxterja | 3:a85b742be262 | 469 | |
baxterja | 3:a85b742be262 | 470 | #define DecimationFactor_10K 10 |
baxterja | 3:a85b742be262 | 471 | void filter1K(float FIR1K_Sample1_i_input, float FIR1K_Sample1_q_input, float FIR1K_Sample2_i_input, float FIR1K_Sample2_q_input) |
baxterja | 3:a85b742be262 | 472 | { |
baxterja | 5:1b2dc43e8947 | 473 | static float Final_Average1_i=0; |
baxterja | 5:1b2dc43e8947 | 474 | static float Final_Average1_q=0;//when decimating sum up all 10 samples at a time have that be your output value |
baxterja | 5:1b2dc43e8947 | 475 | static float Final_Average2_i=0; |
baxterja | 5:1b2dc43e8947 | 476 | static float Final_Average2_q=0; |
baxterja | 5:1b2dc43e8947 | 477 | |
baxterja | 5:1b2dc43e8947 | 478 | |
baxterja | 5:1b2dc43e8947 | 479 | |
baxterja | 3:a85b742be262 | 480 | static uint8_t decimationCounter = 0;//used to keep track of how many samples you have currently decimated |
DeWayneDennis | 6:63de50ac29be | 481 | static uint16_t finalAverageCounter = 0; //used to keep track of how many elements to average across |
baxterja | 3:a85b742be262 | 482 | static float FIR1K_Sample1_i_DecimatedSum=0; |
baxterja | 3:a85b742be262 | 483 | static float FIR1K_Sample1_q_DecimatedSum=0;//when decimating sum up all 10 samples at a time have that be your output value |
baxterja | 3:a85b742be262 | 484 | static float FIR1K_Sample2_i_DecimatedSum=0; |
baxterja | 3:a85b742be262 | 485 | static float FIR1K_Sample2_q_DecimatedSum=0; |
baxterja | 3:a85b742be262 | 486 | |
baxterja | 3:a85b742be262 | 487 | FIR1K_Sample1_i_DecimatedSum += FIR1K_Sample1_i_input;//add sample to the sum of previous sample |
baxterja | 3:a85b742be262 | 488 | FIR1K_Sample1_q_DecimatedSum += FIR1K_Sample1_q_input; |
baxterja | 3:a85b742be262 | 489 | FIR1K_Sample2_i_DecimatedSum += FIR1K_Sample2_i_input; |
baxterja | 3:a85b742be262 | 490 | FIR1K_Sample2_q_DecimatedSum += FIR1K_Sample2_q_input; |
baxterja | 3:a85b742be262 | 491 | decimationCounter++; |
baxterja | 3:a85b742be262 | 492 | if (decimationCounter >= DecimationFactor_10K)//once 10 samples have com |
baxterja | 3:a85b742be262 | 493 | { |
baxterja | 3:a85b742be262 | 494 | decimationCounter = 0;//reset decimation counter |
baxterja | 3:a85b742be262 | 495 | //add sample to 10K filter |
baxterja | 3:a85b742be262 | 496 | FIR1K_Sample1_i[FIR1K_Position] = FIR1K_Sample1_i_DecimatedSum; |
baxterja | 3:a85b742be262 | 497 | FIR1K_Sample1_q[FIR1K_Position] = FIR1K_Sample1_q_DecimatedSum; |
baxterja | 3:a85b742be262 | 498 | FIR1K_Sample2_i[FIR1K_Position] = FIR1K_Sample2_i_DecimatedSum; |
baxterja | 3:a85b742be262 | 499 | FIR1K_Sample2_q[FIR1K_Position] = FIR1K_Sample2_q_DecimatedSum; |
baxterja | 3:a85b742be262 | 500 | |
baxterja | 3:a85b742be262 | 501 | FIR1K_Sample1_i_DecimatedSum = 0;//reset decimated sum to 0 for next sample |
baxterja | 3:a85b742be262 | 502 | FIR1K_Sample1_q_DecimatedSum = 0; |
baxterja | 3:a85b742be262 | 503 | FIR1K_Sample2_i_DecimatedSum = 0; |
baxterja | 3:a85b742be262 | 504 | FIR1K_Sample2_q_DecimatedSum = 0; |
baxterja | 3:a85b742be262 | 505 | |
baxterja | 3:a85b742be262 | 506 | FIR1K_Position++; //increment circular buffer |
baxterja | 3:a85b742be262 | 507 | if (FIR1K_Position >= FIR_1K_LENGTH) //wrap around |
baxterja | 3:a85b742be262 | 508 | { |
baxterja | 3:a85b742be262 | 509 | FIR1K_Position = 0; |
baxterja | 3:a85b742be262 | 510 | } |
baxterja | 3:a85b742be262 | 511 | |
baxterja | 3:a85b742be262 | 512 | // Low pass filter of demodulated signal |
baxterja | 3:a85b742be262 | 513 | float FIR1K_Sample1_i_Output = FIR1K_Sample1_i[FIR1K_Position] * lp_1K_coeff[0];//first multiply of convolution |
baxterja | 3:a85b742be262 | 514 | float FIR1K_Sample1_q_Output = FIR1K_Sample1_q[FIR1K_Position] * lp_1K_coeff[0]; |
baxterja | 3:a85b742be262 | 515 | float FIR1K_Sample2_i_Output = FIR1K_Sample2_i[FIR1K_Position] * lp_1K_coeff[0]; |
baxterja | 3:a85b742be262 | 516 | float FIR1K_Sample2_q_Output = FIR1K_Sample2_q[FIR1K_Position] * lp_1K_coeff[0]; |
baxterja | 3:a85b742be262 | 517 | int fir_index; |
baxterja | 3:a85b742be262 | 518 | for(int fir_counter = 1; fir_counter < FIR_1K_LENGTH; fir_counter++)//the rest of the convolution |
baxterja | 3:a85b742be262 | 519 | { |
baxterja | 3:a85b742be262 | 520 | fir_index = FIR1K_Position + fir_counter; |
baxterja | 3:a85b742be262 | 521 | if (fir_index >= FIR_1K_LENGTH) |
baxterja | 3:a85b742be262 | 522 | { |
baxterja | 3:a85b742be262 | 523 | fir_index -= FIR_1K_LENGTH; |
baxterja | 3:a85b742be262 | 524 | } |
baxterja | 3:a85b742be262 | 525 | FIR1K_Sample1_i_Output += FIR1K_Sample1_i[fir_index] * lp_1K_coeff[fir_counter];//convolving |
baxterja | 3:a85b742be262 | 526 | FIR1K_Sample1_q_Output += FIR1K_Sample1_q[fir_index] * lp_1K_coeff[fir_counter]; |
baxterja | 3:a85b742be262 | 527 | FIR1K_Sample2_i_Output += FIR1K_Sample2_i[fir_index] * lp_1K_coeff[fir_counter]; |
baxterja | 3:a85b742be262 | 528 | FIR1K_Sample2_q_Output += FIR1K_Sample2_q[fir_index] * lp_1K_coeff[fir_counter]; |
baxterja | 3:a85b742be262 | 529 | } |
baxterja | 5:1b2dc43e8947 | 530 | Final_Average1_i+=FIR1K_Sample1_i_Output; |
baxterja | 5:1b2dc43e8947 | 531 | Final_Average1_q+=FIR1K_Sample1_q_Output;//when decimating sum up all 10 samples at a time have that be your output value |
baxterja | 5:1b2dc43e8947 | 532 | Final_Average2_i+=FIR1K_Sample2_i_Output; |
baxterja | 5:1b2dc43e8947 | 533 | Final_Average2_q+=FIR1K_Sample2_q_Output; |
baxterja | 5:1b2dc43e8947 | 534 | finalAverageCounter++; |
baxterja | 5:1b2dc43e8947 | 535 | if (finalAverageCounter>=NUMSAMPLESAVERAGE) |
baxterja | 5:1b2dc43e8947 | 536 | { |
baxterja | 5:1b2dc43e8947 | 537 | finalAverageCounter=0; |
baxterja | 5:1b2dc43e8947 | 538 | float mag1 = sqrt(Final_Average1_i*Final_Average1_i+Final_Average1_q*Final_Average1_q); |
baxterja | 5:1b2dc43e8947 | 539 | float mag2 = sqrt(Final_Average2_i*Final_Average2_i+Final_Average2_q*Final_Average2_q); |
DeWayneDennis | 6:63de50ac29be | 540 | //store filtered values for later reading |
DeWayneDennis | 6:63de50ac29be | 541 | filteredLong = mag1; |
DeWayneDennis | 6:63de50ac29be | 542 | filteredLongRef = mag2; |
DeWayneDennis | 6:63de50ac29be | 543 | |
baxterja | 5:1b2dc43e8947 | 544 | Final_Average1_i=0; |
baxterja | 5:1b2dc43e8947 | 545 | Final_Average1_q=0;//when decimating sum up all 10 samples at a time have that be your output value |
baxterja | 5:1b2dc43e8947 | 546 | Final_Average2_i=0; |
baxterja | 5:1b2dc43e8947 | 547 | Final_Average2_q=0; |
baxterja | 5:1b2dc43e8947 | 548 | } |
baxterja | 3:a85b742be262 | 549 | //float mag1 = sqrt(FIR1K_Sample1_i_Output*FIR1K_Sample1_i_Output+FIR1K_Sample1_q_Output*FIR1K_Sample1_q_Output); |
baxterja | 3:a85b742be262 | 550 | //float mag2 = sqrt(FIR1K_Sample2_i_Output*FIR1K_Sample2_i_Output+FIR1K_Sample2_q_Output*FIR1K_Sample2_q_Output); |
baxterja | 3:a85b742be262 | 551 | //printf("V1: %f\tV2: %f\n\r",mag1,mag2); |
baxterja | 5:1b2dc43e8947 | 552 | //filter100(FIR1K_Sample1_i_Output, FIR1K_Sample1_q_Output, FIR1K_Sample2_i_Output, FIR1K_Sample2_q_Output); |
baxterja | 3:a85b742be262 | 553 | } |
baxterja | 3:a85b742be262 | 554 | } |
baxterja | 3:a85b742be262 | 555 | |
baxterja | 3:a85b742be262 | 556 | |
baxterja | 3:a85b742be262 | 557 | |
baxterja | 3:a85b742be262 | 558 | #define FIR_10K_LENGTH 16 |
baxterja | 3:a85b742be262 | 559 | float FIR10K_Sample1_i[FIR_10K_LENGTH]; |
baxterja | 3:a85b742be262 | 560 | float FIR10K_Sample1_q[FIR_10K_LENGTH]; |
baxterja | 3:a85b742be262 | 561 | float FIR10K_Sample2_i[FIR_10K_LENGTH]; |
baxterja | 3:a85b742be262 | 562 | float FIR10K_Sample2_q[FIR_10K_LENGTH]; |
baxterja | 3:a85b742be262 | 563 | uint8_t FIR10K_Position = 0; |
baxterja | 3:a85b742be262 | 564 | //Fs = 10000, Order = 15, Fpass = 10, Fstop = 900 |
baxterja | 3:a85b742be262 | 565 | static float lp_10K_coeff[FIR_10K_LENGTH] = { |
baxterja | 3:a85b742be262 | 566 | 0.0242947345184044, |
baxterja | 3:a85b742be262 | 567 | 0.0283599756767857, |
baxterja | 3:a85b742be262 | 568 | 0.0416004471421328, |
baxterja | 3:a85b742be262 | 569 | 0.0557840684332377, |
baxterja | 3:a85b742be262 | 570 | 0.0695867614174704, |
baxterja | 3:a85b742be262 | 571 | 0.0816182734401924, |
baxterja | 3:a85b742be262 | 572 | 0.0904748943926879, |
baxterja | 3:a85b742be262 | 573 | 0.0951919735506062, |
baxterja | 3:a85b742be262 | 574 | 0.0951919735506062, |
baxterja | 3:a85b742be262 | 575 | 0.0904748943926879, |
baxterja | 3:a85b742be262 | 576 | 0.0816182734401924, |
baxterja | 3:a85b742be262 | 577 | 0.0695867614174704, |
baxterja | 3:a85b742be262 | 578 | 0.0557840684332377, |
baxterja | 3:a85b742be262 | 579 | 0.0416004471421328, |
baxterja | 3:a85b742be262 | 580 | 0.0283599756767857, |
baxterja | 3:a85b742be262 | 581 | 0.0242947345184044 |
baxterja | 3:a85b742be262 | 582 | }; |
baxterja | 3:a85b742be262 | 583 | |
baxterja | 3:a85b742be262 | 584 | #define DecimationFactor_100K 10 |
baxterja | 3:a85b742be262 | 585 | void filter10K(float FIR10K_Sample1_i_input, float FIR10K_Sample1_q_input, float FIR10K_Sample2_i_input, float FIR10K_Sample2_q_input) |
baxterja | 3:a85b742be262 | 586 | { |
baxterja | 3:a85b742be262 | 587 | static uint8_t decimationCounter = 0;//used to keep track of how many samples you have currently decimated |
baxterja | 3:a85b742be262 | 588 | static float FIR10K_Sample1_i_DecimatedSum=0; |
baxterja | 3:a85b742be262 | 589 | static float FIR10K_Sample1_q_DecimatedSum=0;//when decimating sum up all 10 samples at a time have that be your output value |
baxterja | 3:a85b742be262 | 590 | static float FIR10K_Sample2_i_DecimatedSum=0; |
baxterja | 3:a85b742be262 | 591 | static float FIR10K_Sample2_q_DecimatedSum=0; |
baxterja | 3:a85b742be262 | 592 | |
baxterja | 3:a85b742be262 | 593 | FIR10K_Sample1_i_DecimatedSum += FIR10K_Sample1_i_input;//add sample to the sum of previous sample |
baxterja | 3:a85b742be262 | 594 | FIR10K_Sample1_q_DecimatedSum += FIR10K_Sample1_q_input; |
baxterja | 3:a85b742be262 | 595 | FIR10K_Sample2_i_DecimatedSum += FIR10K_Sample2_i_input; |
baxterja | 3:a85b742be262 | 596 | FIR10K_Sample2_q_DecimatedSum += FIR10K_Sample2_q_input; |
baxterja | 3:a85b742be262 | 597 | decimationCounter++; |
baxterja | 3:a85b742be262 | 598 | if (decimationCounter >= DecimationFactor_100K)//once 10 samples have com |
baxterja | 3:a85b742be262 | 599 | { |
baxterja | 3:a85b742be262 | 600 | decimationCounter = 0;//reset decimation counter |
baxterja | 3:a85b742be262 | 601 | //add sample to 10K filter |
baxterja | 3:a85b742be262 | 602 | FIR10K_Sample1_i[FIR10K_Position] = FIR10K_Sample1_i_DecimatedSum; |
baxterja | 3:a85b742be262 | 603 | FIR10K_Sample1_q[FIR10K_Position] = FIR10K_Sample1_q_DecimatedSum; |
baxterja | 3:a85b742be262 | 604 | FIR10K_Sample2_i[FIR10K_Position] = FIR10K_Sample2_i_DecimatedSum; |
baxterja | 3:a85b742be262 | 605 | FIR10K_Sample2_q[FIR10K_Position] = FIR10K_Sample2_q_DecimatedSum; |
baxterja | 3:a85b742be262 | 606 | |
baxterja | 3:a85b742be262 | 607 | FIR10K_Sample1_i_DecimatedSum = 0;//reset decimated sum to 0 for next sample |
baxterja | 3:a85b742be262 | 608 | FIR10K_Sample1_q_DecimatedSum = 0; |
baxterja | 3:a85b742be262 | 609 | FIR10K_Sample2_i_DecimatedSum = 0; |
baxterja | 3:a85b742be262 | 610 | FIR10K_Sample2_q_DecimatedSum = 0; |
baxterja | 3:a85b742be262 | 611 | |
baxterja | 3:a85b742be262 | 612 | FIR10K_Position++; //increment circular buffer |
baxterja | 3:a85b742be262 | 613 | if (FIR10K_Position >= FIR_10K_LENGTH) //wrap around |
baxterja | 3:a85b742be262 | 614 | { |
baxterja | 3:a85b742be262 | 615 | FIR10K_Position = 0; |
baxterja | 3:a85b742be262 | 616 | } |
baxterja | 3:a85b742be262 | 617 | |
baxterja | 3:a85b742be262 | 618 | // Low pass filter of demodulated signal |
baxterja | 3:a85b742be262 | 619 | float FIR10K_Sample1_i_Output = FIR10K_Sample1_i[FIR10K_Position] * lp_10K_coeff[0];//first multiply of convolution |
baxterja | 3:a85b742be262 | 620 | float FIR10K_Sample1_q_Output = FIR10K_Sample1_q[FIR10K_Position] * lp_10K_coeff[0]; |
baxterja | 3:a85b742be262 | 621 | float FIR10K_Sample2_i_Output = FIR10K_Sample2_i[FIR10K_Position] * lp_10K_coeff[0]; |
baxterja | 3:a85b742be262 | 622 | float FIR10K_Sample2_q_Output = FIR10K_Sample2_q[FIR10K_Position] * lp_10K_coeff[0]; |
baxterja | 3:a85b742be262 | 623 | int fir_index; |
baxterja | 3:a85b742be262 | 624 | for(int fir_counter = 1; fir_counter < FIR_10K_LENGTH; fir_counter++)//the rest of the convolution |
baxterja | 3:a85b742be262 | 625 | { |
baxterja | 3:a85b742be262 | 626 | fir_index = FIR10K_Position + fir_counter; |
baxterja | 3:a85b742be262 | 627 | if (fir_index >= FIR_10K_LENGTH) |
baxterja | 3:a85b742be262 | 628 | { |
baxterja | 3:a85b742be262 | 629 | fir_index -= FIR_10K_LENGTH; |
baxterja | 3:a85b742be262 | 630 | } |
baxterja | 3:a85b742be262 | 631 | FIR10K_Sample1_i_Output += FIR10K_Sample1_i[fir_index] * lp_10K_coeff[fir_counter];//convolving |
baxterja | 3:a85b742be262 | 632 | FIR10K_Sample1_q_Output += FIR10K_Sample1_q[fir_index] * lp_10K_coeff[fir_counter]; |
baxterja | 3:a85b742be262 | 633 | FIR10K_Sample2_i_Output += FIR10K_Sample2_i[fir_index] * lp_10K_coeff[fir_counter]; |
baxterja | 3:a85b742be262 | 634 | FIR10K_Sample2_q_Output += FIR10K_Sample2_q[fir_index] * lp_10K_coeff[fir_counter]; |
baxterja | 3:a85b742be262 | 635 | } |
baxterja | 3:a85b742be262 | 636 | //float mag1 = sqrt(FIR10K_Sample1_i_Output*FIR10K_Sample1_i_Output+FIR10K_Sample1_q_Output*FIR10K_Sample1_q_Output); |
baxterja | 3:a85b742be262 | 637 | //float mag2 = sqrt(FIR10K_Sample2_i_Output*FIR10K_Sample2_i_Output+FIR10K_Sample2_q_Output*FIR10K_Sample2_q_Output); |
baxterja | 3:a85b742be262 | 638 | //printf("P1: %d\tV1: %f\tV2: %f\n\r",phase_counter,mag1,mag2); |
baxterja | 3:a85b742be262 | 639 | //printf("V1: %f\tV2: %f\n\r",mag1,mag2); |
baxterja | 3:a85b742be262 | 640 | filter1K(FIR10K_Sample1_i_Output, FIR10K_Sample1_q_Output, FIR10K_Sample2_i_Output, FIR10K_Sample2_q_Output); |
baxterja | 3:a85b742be262 | 641 | } |
baxterja | 3:a85b742be262 | 642 | } |
baxterja | 3:a85b742be262 | 643 | |
baxterja | 3:a85b742be262 | 644 | |
baxterja | 3:a85b742be262 | 645 | |
baxterja | 3:a85b742be262 | 646 | |
baxterja | 3:a85b742be262 | 647 | |
baxterja | 3:a85b742be262 | 648 | #define FIR_100K_LENGTH 16 |
baxterja | 3:a85b742be262 | 649 | float FIR100K_Sample1_i[FIR_100K_LENGTH]; |
baxterja | 3:a85b742be262 | 650 | float FIR100K_Sample1_q[FIR_100K_LENGTH]; |
baxterja | 3:a85b742be262 | 651 | float FIR100K_Sample2_i[FIR_100K_LENGTH]; |
baxterja | 3:a85b742be262 | 652 | float FIR100K_Sample2_q[FIR_100K_LENGTH]; |
baxterja | 3:a85b742be262 | 653 | |
baxterja | 3:a85b742be262 | 654 | uint8_t FIR100K_Position = 0; |
baxterja | 3:a85b742be262 | 655 | //Fs = 100000, Order = 15, Fpass = 100, Fstop = 9000 |
baxterja | 3:a85b742be262 | 656 | static float lp_100K_coeff[FIR_100K_LENGTH] = { |
baxterja | 4:9fd291254686 | 657 | 0.0102922869776514, |
baxterja | 4:9fd291254686 | 658 | 0.0196357484226367, |
baxterja | 4:9fd291254686 | 659 | 0.0346068275004222, |
baxterja | 4:9fd291254686 | 660 | 0.0528964828542786, |
baxterja | 4:9fd291254686 | 661 | 0.0725363140207878, |
baxterja | 4:9fd291254686 | 662 | 0.0908817555678698, |
baxterja | 4:9fd291254686 | 663 | 0.105122693390187, |
baxterja | 4:9fd291254686 | 664 | 0.112904816559370, |
baxterja | 4:9fd291254686 | 665 | 0.112904816559370, |
baxterja | 4:9fd291254686 | 666 | 0.105122693390187, |
baxterja | 4:9fd291254686 | 667 | 0.0908817555678698, |
baxterja | 4:9fd291254686 | 668 | 0.0725363140207878, |
baxterja | 4:9fd291254686 | 669 | 0.0528964828542786, |
baxterja | 4:9fd291254686 | 670 | 0.0346068275004222, |
baxterja | 4:9fd291254686 | 671 | 0.0196357484226367, |
baxterja | 4:9fd291254686 | 672 | 0.0102922869776514 |
baxterja | 3:a85b742be262 | 673 | }; |
baxterja | 3:a85b742be262 | 674 | |
baxterja | 3:a85b742be262 | 675 | void filter100K(int sample1, int sample2) |
baxterja | 3:a85b742be262 | 676 | { |
baxterja | 3:a85b742be262 | 677 | float current_i_mod = i_mod_pre[phase_counter];//sin and cos to demodulate signature |
baxterja | 3:a85b742be262 | 678 | float current_q_mod = q_mod_pre[phase_counter]; |
baxterja | 3:a85b742be262 | 679 | phase_counter++;//increment the demodulating sinusoids by 1/Fs |
baxterja | 3:a85b742be262 | 680 | |
baxterja | 3:a85b742be262 | 681 | if (phase_counter>=pre_compute_length)//wrap around to beginning of sin wave |
baxterja | 3:a85b742be262 | 682 | { |
baxterja | 3:a85b742be262 | 683 | phase_counter = 0; |
baxterja | 3:a85b742be262 | 684 | } |
baxterja | 3:a85b742be262 | 685 | |
baxterja | 3:a85b742be262 | 686 | FIR100K_Sample1_i[FIR100K_Position] = (sample1 * current_i_mod);//this centers the delta function at f =0 in z space |
baxterja | 3:a85b742be262 | 687 | FIR100K_Sample1_q[FIR100K_Position] = (sample1 * current_q_mod);//this centers the delta function at f =0 in z space |
baxterja | 3:a85b742be262 | 688 | FIR100K_Sample2_i[FIR100K_Position] = (sample2 * current_i_mod);//this centers the delta function at f =0 in z space |
baxterja | 3:a85b742be262 | 689 | FIR100K_Sample2_q[FIR100K_Position] = (sample2 * current_q_mod);//this centers the delta function at f =0 in z space |
baxterja | 3:a85b742be262 | 690 | |
baxterja | 3:a85b742be262 | 691 | FIR100K_Position++;//increment position of circular buffer |
baxterja | 3:a85b742be262 | 692 | if (FIR100K_Position >= FIR_100K_LENGTH)//wrap around at end of buffer |
baxterja | 3:a85b742be262 | 693 | { |
baxterja | 3:a85b742be262 | 694 | FIR100K_Position = 0; |
baxterja | 3:a85b742be262 | 695 | } |
baxterja | 3:a85b742be262 | 696 | |
baxterja | 3:a85b742be262 | 697 | // Low pass filter of demodulated signal |
baxterja | 3:a85b742be262 | 698 | float FIR100K_Sample1_i_Output = FIR100K_Sample1_i[FIR100K_Position] * lp_100K_coeff[0];//first multiply in convolution |
baxterja | 3:a85b742be262 | 699 | float FIR100K_Sample1_q_Output = FIR100K_Sample1_q[FIR100K_Position] * lp_100K_coeff[0];//first multiply in convolution |
baxterja | 3:a85b742be262 | 700 | float FIR100K_Sample2_i_Output = FIR100K_Sample2_i[FIR100K_Position] * lp_100K_coeff[0];//first multiply in convolution |
baxterja | 3:a85b742be262 | 701 | float FIR100K_Sample2_q_Output = FIR100K_Sample2_q[FIR100K_Position] * lp_100K_coeff[0];//first multiply in convolution |
baxterja | 3:a85b742be262 | 702 | int fir_index; |
baxterja | 3:a85b742be262 | 703 | for(int fir_counter = 1; fir_counter < FIR_100K_LENGTH; fir_counter++)//convolves the filter with the signal |
baxterja | 3:a85b742be262 | 704 | { |
baxterja | 3:a85b742be262 | 705 | fir_index = FIR100K_Position + fir_counter; |
baxterja | 3:a85b742be262 | 706 | if (fir_index >= FIR_100K_LENGTH)//wrap around |
baxterja | 3:a85b742be262 | 707 | { |
baxterja | 3:a85b742be262 | 708 | fir_index -= FIR_100K_LENGTH; |
baxterja | 3:a85b742be262 | 709 | } |
baxterja | 3:a85b742be262 | 710 | FIR100K_Sample1_i_Output += FIR100K_Sample1_i[fir_index] * lp_100K_coeff[fir_counter];//convolution |
baxterja | 3:a85b742be262 | 711 | FIR100K_Sample1_q_Output += FIR100K_Sample1_q[fir_index] * lp_100K_coeff[fir_counter]; |
baxterja | 3:a85b742be262 | 712 | FIR100K_Sample2_i_Output += FIR100K_Sample2_i[fir_index] * lp_100K_coeff[fir_counter]; |
baxterja | 3:a85b742be262 | 713 | FIR100K_Sample2_q_Output += FIR100K_Sample2_q[fir_index] * lp_100K_coeff[fir_counter]; |
baxterja | 3:a85b742be262 | 714 | } |
baxterja | 3:a85b742be262 | 715 | //pass data to next filter |
baxterja | 3:a85b742be262 | 716 | //float mag1 = sqrt(FIR100K_Sample1_i_Output*FIR100K_Sample1_i_Output+FIR100K_Sample1_q_Output*FIR100K_Sample1_q_Output); |
baxterja | 3:a85b742be262 | 717 | //float mag2 = sqrt(FIR100K_Sample2_i_Output*FIR100K_Sample2_i_Output+FIR100K_Sample2_q_Output*FIR100K_Sample2_q_Output); |
baxterja | 3:a85b742be262 | 718 | //printf("V1: %f\tV2: %f\n\r",mag1,mag2); |
baxterja | 3:a85b742be262 | 719 | filter10K(FIR100K_Sample1_i_Output, FIR100K_Sample1_q_Output, FIR100K_Sample2_i_Output, FIR100K_Sample2_q_Output); |
baxterja | 3:a85b742be262 | 720 | } |
baxterja | 3:a85b742be262 | 721 | |
DeWayneDennis | 6:63de50ac29be | 722 | float getFiltered(){ |
DeWayneDennis | 6:63de50ac29be | 723 | return filteredLong; |
DeWayneDennis | 6:63de50ac29be | 724 | } |
DeWayneDennis | 6:63de50ac29be | 725 | float getFilteredRef(){ |
DeWayneDennis | 6:63de50ac29be | 726 | return filteredLongRef; |
DeWayneDennis | 6:63de50ac29be | 727 | } |