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