-
Notifications
You must be signed in to change notification settings - Fork 1
/
DSP_and_Math.c
1293 lines (1121 loc) · 49.9 KB
/
DSP_and_Math.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* DSP_and_Math - Library with useful DSP and math functions - .c file
* - some functions using float point and standard math.h lib
* - some functions using fixed notation to (optimized)
*
* author: Haroldo Amaral - [email protected]
* v0.4.3 - 2017/10/09
******************************************************************************
* log:
* v0.1 . Initial version
* + add rms functions and structures
* + add dc filter float version and structures
* + add dc filter fixed version and structures
* + add dc filter fixed extended version and structures
* v0.2 . rename dc filter to iir_hipassfilter
* . rename associated structures
* + add iir low pass filter and structures
* v0.3 . change name of rms functions
* + add new rms functions
* + add sqrt_Int32 and sqrt_Int64 (integer versions)
* v0.4 . change volatile variables
* . optimize sqrt_Int32 by defines
* . change rms_valueadd input parameter (pass the value instead of pointer)
* - remove sqrt_Int64 - not efficient
* - remove rms int32 functions - not efficient
* + add sine wave gen function
* + add rmsClearStruct to function sample by sample
* + add Goertzel functions (array and sample-by-sample)
* v0.4.1 . improve efficiency on "goertzelArrayInt16_Fixed64()"
* - remove old remain functions
* v0.4.2 . fix "sineWaveGen_GetSample()" function - phase error
* v0.4.3 . organized defines
******************************************************************************/
#include "DSP_and_Math.h"
#include "math.h"
/******************************************************************************
* MATH FUNCTIONS
******************************************************************************/
/******************************************************************************
* Calculate the Square root of a 32 bit signed number
* - will return "-1" if the number is negative
*
* - INPUT: int32_t x (32 bit signed input number)
*
* - RETURN: root (integer square root of x)
*
* Reference: http://www.codecodex.com/wiki/Calculate_an_integer_square_root
******************************************************************************/
int32_t sqrt_Int32(int32_t x)
{
register uint32_t root, remainder, place;
/* verify if number is negative */
if (x < 0)
{
return (-1); // return "-1" - error
}
/* verify if number is zero */
if (x == 0)
{
return (0);
}
root = 0;
remainder = x;
place = 0x40000000; // to 32 bit input
while (place > remainder)
place = place >> 2;
while (place)
{
if (remainder >= root + place)
{
remainder = remainder - root - place;
root = root + (place << 1);
}
root = root >> 1;
place = place >> 2;
}
return root;
}
/******************************************************************************
* Calculate the RMS value of N sample of a float array
* - use float variables to accumulate and return the result in float
*
* - INPUT: const float * arrayIn (pointer to array with the samples)
* uint_fast16_t size (number of samples - limited to 65535)
* float dcLevel (previously calculated dc level)
*
* - RETURN: calculated RMS value (float)
******************************************************************************/
float rmsValueArray_Float_StdMath(const float * arrayIn, uint_fast16_t size, float dcLevel)
{
uint_fast16_t counter;
float sample_temp;
float acc = 0;
/* check if there is a dc leve (different of zero) */
if (dcLevel)
{
for (counter = 0; counter < size; counter++)
{
sample_temp = arrayIn[counter] - dcLevel; // subtraction of dc level before square
acc += (sample_temp * sample_temp); // square and accumulate
}
}
/* if dc level is zero, the subtraction can be removed */
else
{
for (counter = 0; counter < size; counter++)
{
sample_temp = arrayIn[counter]; // save the sample
acc += (sample_temp * sample_temp); // square and accumulate
}
}
/*
* calculate the average and then extract square root - RMS value
*/
acc /= (float)size;
return (float)sqrtf(acc);
}
/******************************************************************************
* Calculate the RMS value of N sample of a int16_t array
* - use integer (32 bits) variables to accumulate and return the result in float
* - limitations about the input limit during math stage
*
* - INPUT: const float * arrayIn (pointer to array with the samples)
* uint_fast16_t size (number of samples - limited to 65535)
* float dcLevel (previously calculated dc level)
*
* - RETURN: calculated RMS value (float)
******************************************************************************/
float rmsValueArray_Int16_StdMath(const int16_t * arrayIn, uint_fast16_t size, int16_t dcLevel)
{
uint_fast16_t counter;
int32_t sample_temp;
uint32_t acc = 0;
/* check if there is a dc leve (different of zero) */
if (dcLevel)
{
for (counter = 0; counter < size; counter++)
{
sample_temp = (int32_t)(arrayIn[counter] - dcLevel); // subtraction of dc level before square
acc = acc + (uint32_t)(sample_temp * sample_temp); // square and accumulate
}
}
/* if dc level is zero, the subtraction can be removed */
else
{
for (counter = 0; counter < size; counter++)
{
sample_temp = (int32_t)arrayIn[counter]; // save the sample
acc = acc + (uint32_t)(sample_temp * sample_temp); // square and accumulate
}
}
/*
* calculate the average and then extract square root - RMS value
*/
#if defined (RMS_ARRAY_STD)
/************************************************************
* version using std math.h square root function
************************************************************/
float result;
result = (float)acc/size;
return sqrtf(result);
#elif defined(RMS_ARRAY_OPTIMIZED)
/************************************************************
* version using integer square root function
************************************************************/
uint32_t result;
result = acc/size;
/************************************************************
* To reach better accuracy, shift the result when possible
* - transform in a fixed point math
************************************************************/
if (result & (3UL << 30)) // verify if bit 30 and 31 is true
{
result = sqrt_Int32(result); // if yes, do the sqrt
return (float)(result);
}
else if (result & (3UL << 28)) // verify bits 28 and 29
{
result = sqrt_Int32((result) << 1); // rotate 1x to left == multiply by 2
return (float)(result/1.414213f); // divide the result by sqrt of 2
}
else if (result & (15UL << 24)) // verify bits between 24 and 27
{
result = sqrt_Int32((result) << 3); // rotate 3x to left == multiply by 2
return (float)(result/2.828427f); // divide the result by sqrt of 8
}
else if (result & (15UL << 20))
{
result = sqrt_Int32((result) << 7);
return (float)(result/11.313708f);
}
else if (result & (15UL << 16))
{
result = sqrt_Int32((result) << 11);
return (float)(result/45.254834f);
}
else if (result & (15UL << 12))
{
result = sqrt_Int32((result) << 15);
return (float)(result/181.019336f);
}
else if (result & (15UL << 8))
{
result = sqrt_Int32((result) << 19);
return (float)(result/724.077343f);
}
else
{
result = sqrt_Int32((result) << 23);
return (float)(result/2896.309376f);
}
#else
#error "RMS Array Int16 - invalid option, select one define!"
#endif
}
/******************************************************************************
* Add sample to accumulator - Allow to calculate RMS value sample by sample
* - square input values and accumulate
* - Finalizing calculations in a separate function
*
* - INPUT: rms_float_t * inputStruct (pointer to struct with RMS parameters)
* const float * sampleFloat (pointer to float sample)
*
* - RETURN: N/A (result returned inside the struct)
******************************************************************************/
void rmsValueAddSample_Float(rms_float_t * inputStruct, float sample)
{
/* square value and accumulate */
inputStruct->acc += (sample * sample);
/* increment counter - used in final step */
inputStruct->size_counter++;
}
/******************************************************************************
* Add sample to accumulator - Allow to calculate RMS value sample by sample
* - square input values and accumulate
* - Finalizing calculations in a separate function
*
* - INPUT: rms_int16_t * inputStruct (pointer to struct with RMS parameters)
* const float * sampleFloat (pointer to float sample)
*
* - RETURN: N/A (result returned inside the struct)
******************************************************************************/
void rmsValueAddSample_Int16(rms_int16_t * inputStruct, int16_t sample)
{
int32_t sample_temp;
sample_temp = (int32_t)sample; // save the sample
/* square value and accumulate */
inputStruct->acc = inputStruct->acc + (uint32_t)(sample_temp * sample_temp); // square and accumulate
/* increment counter - used in final step */
inputStruct->size_counter++;
}
/******************************************************************************
* Clear RMS Float Struct - reset all parameters (variables)
* - enable to clear some rms calculation without do the math
*
* - INPUT: rms_float_t * inputStruct (pointer to struct with RMS parameters)
*
* - RETURN: N/A
******************************************************************************/
void rmsClearStruct_Float(rms_float_t * inputStruct)
{
inputStruct->acc = 0;
inputStruct->rmsValue = 0;
inputStruct->size_counter = 0;
}
/******************************************************************************
* Clear RMS Int16 Struct - reset all parameters (variables)
* - enable to clear some rms calculation without do the math
*
* - INPUT: rms_int16_t * inputStruct (pointer to struct with RMS parameters)
*
* - RETURN: N/A
******************************************************************************/
void rmsClearStruct_Int16(rms_int16_t * inputStruct)
{
inputStruct->acc = 0;
inputStruct->rmsValue = 0;
inputStruct->size_counter = 0;
}
/******************************************************************************
* Finalize RMS calculation based in previously added samples
* - calculate the average and root square
*
* - INPUT: rms_float_t * inputStruct (pointer to struct with RMS parameters)
*
* - RETURN: N/A (result returned inside the struct)
******************************************************************************/
void rmsValueCalcRmsStdMath_Float(rms_float_t * inputStruct)
{
/*
* Finalize the math
* - calculate the average and then extract square root - RMS value
*/
inputStruct->rmsValue = inputStruct->acc / inputStruct->size_counter;
inputStruct->rmsValue = sqrtf(inputStruct->rmsValue);
inputStruct->acc = 0; // clear accumulator
inputStruct->size_counter = 0; // clear counter
}
/******************************************************************************
* Finalize RMS calculation based in previously added samples
* - calculate the average and root square
*
* - INPUT: rms_float_t * inputStruct (pointer to struct with RMS parameters)
*
* - RETURN: N/A (result returned inside the struct)
******************************************************************************/
void rmsValueCalcRmsStdMath_Int16(rms_int16_t * inputStruct)
{
/*
* Finalize the math
* - calculate the average and then extract square root - RMS value
*/
#if defined (RMS_SAMPLE_STD)
volatile float result;
result = (float)inputStruct->acc / inputStruct->size_counter;
inputStruct->rmsValue = sqrtf(result);
inputStruct->acc = 0; // clear accumulator
inputStruct->size_counter = 0; // clear counter
#elif defined(RMS_SAMPLE_OPTIMIZED)
uint32_t result;
result = inputStruct->acc/inputStruct->size_counter;
if (result & (3UL << 30))
{
result = sqrt_Int32(result);
inputStruct->rmsValue = result;
}
else if (result & (3UL << 28))
{
result = sqrt_Int32((result) << 1);
inputStruct->rmsValue = (float)(result/1.414213f);
}
else if (result & (15UL << 24))
{
result = sqrt_Int32((result) << 3);
inputStruct->rmsValue = (float)(result/2.828427f);
}
else if (result & (15UL << 20))
{
result = sqrt_Int32((result) << 7);
inputStruct->rmsValue = (float)(result/11.313708f);
}
else if (result & (15UL << 16))
{
result = sqrt_Int32((result) << 11);
inputStruct->rmsValue = (float)(result/45.254834f);
}
else if (result & (15UL << 12))
{
result = sqrt_Int32((result) << 15);
inputStruct->rmsValue = (float)(result/181.019336f);
}
else if (result & (15UL << 8))
{
result = sqrt_Int32((result) << 19);
inputStruct->rmsValue = (float)(result/724.077343f);
}
else
{
result = sqrt_Int32((result) << 23);
inputStruct->rmsValue = (float)(result/2896.309376f);
}
#else
#error "RMS Sample by Sample Int16 - invalid option, select one define!"
#endif
}
/******************************************************************************
* Sine wave generator - array version
* - using an Array
* - doClean enable to generate waves with harmonics by reusing the array
*
* - INPUT: float * outputArray (array to store samples)
* float freq (frequency of signal)
* float phase_rad (phase displacement in rad)
* float amplitude (amplitude of wave - peak value)
* float V_offset (offset value)
* uint_fast16_t points (points peer cycle)
* uint_fast8_t doClean (clean the array before gen)
*
* - RETURN: N/A
******************************************************************************/
void sineWaveGen_Array_Float(float * outputArray, float freq, float phase_rad,
float amplitude, float V_offset, uint_fast16_t points, uint_fast8_t doClean)
{
uint_fast16_t counter;
/* clean array before calculate new samples */
if (doClean)
{
for (counter = 0; counter < points; counter++)
{
outputArray[counter] = 0; // reset all array
}
}
float increment = (TWO_PI * freq)/points; // samples interval in rad
float x = 0;
for (counter = 0; counter < points; counter++)
{
outputArray[counter] += (amplitude * sinf(x + phase_rad)) + V_offset;
x += increment;
}
}
/******************************************************************************
* Sine wave generator - Initialize struct parameters
* - sample-by-sample version - generate wave on the fly
*
* - INPUT: sine_wave_parameters *inputParameters (struct with parameters)
* float freq (frequency of signal)
* float phase_rad (phase displacement in rad)
* float amplitude (amplitude of wave - peak value)
* float V_offset (offset value)
* uint_fast16_t points (points peer cycle)
* uint_fast8_t doClean (clean the accumulator - reset wave)
*
* - RETURN: N/A (result returned inside the struct)
******************************************************************************/
void sineWaveGen_bySample_Init(sine_wave_parameters *inputParameters, float freq, float phase, float amp, float v_off, uint_fast16_t points, uint_fast8_t doClean)
{
inputParameters->freq = freq;
inputParameters->phase_rad = phase;
inputParameters->amplitude = amp;
inputParameters->V_offset = v_off;
inputParameters->points = points;
inputParameters->increment = (TWO_PI * freq)/points; // samples interval in rad
if (doClean)
{
inputParameters->acc = 0;
}
}
/******************************************************************************
* Sine wave generator - Calculate the current sample
*
* - INPUT: sine_wave_parameters *inputParameters (struct with parameters)
*
* - RETURN: (float)WaveSample (current sample)
******************************************************************************/
float sineWaveGen_GetSample(sine_wave_parameters *inputParameters)
{
float WaveSample = 0;
float sine_param = inputParameters->acc + inputParameters->phase_rad;
/* calculate the sample */
WaveSample = inputParameters->amplitude * sinf(sine_param) + inputParameters->V_offset;
inputParameters->acc += inputParameters->increment; // increment the accumulator
return WaveSample;
}
/******************************************************************************
* DSP FUNCTIONS
******************************************************************************/
/******************************************************************************
* IIR Single Pole High Pass - Float Version - Initialization
*
* - INPUT: iirHighPassFloat_t * structInput (pointer to struct with filter parameters)
* float cutoffFreq (pole value)
* uint_fast8_t doClean (clean variables after change the pole)
*
* - RETURN: N/A (result returned inside the struct)
******************************************************************************/
void iir_SinglePoleHighPass_Float_Init(iirHighPassFloat_t * structInput, float cutoffFreq, uint_fast8_t doClean)
{
structInput->cutoff_Freq = (1.0f - cutoffFreq);
if (doClean)
{
structInput->prev_x = 0;
structInput->prev_y = 0;
structInput->y = 0;
}
}
/******************************************************************************
* IIR Single Pole High Pass - Float Version
* - use float math
* - more efficient with FPU
*
* - INPUT: iirHighPassFloat_t * structInput (pointer to struct with filter parameters)
* float xValueFloat (input/sample value)
*
* - RETURN: N/A (result returned inside the struct)
*
* Reference: https://www.dsprelated.com/freebooks/filters/DC_Blocker.html
******************************************************************************/
void iir_SinglePoleHighPass_Float(iirHighPassFloat_t * structInput, float xValueFloat)
{
/********************************
* y = x - xm1 + (0.995 * ym1);
* xm1 = x;
* ym1 = y;
********************************/
structInput->y = xValueFloat - structInput->prev_x + (structInput->cutoff_Freq * structInput->prev_y);
structInput->prev_x = xValueFloat;
structInput->prev_y = structInput->y;
}
/******************************************************************************
* IIR Single Pole High Pass - Fixed Version Initialization
*
* - INPUT: iirHighPassFixed_t * structInput (pointer to struct with filter parameters)
* float cutoffFreq (pole value)
* uint_fast8_t shift (shift of fixed math - from 8 to 15)
* uint_fast8_t doClean (clean variables after change the pole)
*
* - RETURN: N/A (result returned inside the struct)
******************************************************************************/
void iir_SinglePoleHighPass_Fixed_Init(iirHighPassFixed_t * structInput, float cutoffFreq, uint_fast8_t shift, uint_fast8_t doClean)
{
uint_fast8_t shift_temp = shift;
/*
* prevent shift bigger than 15 (filter stop to work)
*/
if (shift_temp > 15)
{
structInput->shift_size = 15;
}
else if(shift_temp < 8)
{
structInput->shift_size = 8;
}
else
{
structInput->shift_size = shift_temp;
}
structInput->cutoff_Freq = cutoffFreq;
structInput->A_param = (int32_t)((1u << structInput->shift_size) * (structInput->cutoff_Freq));
if (doClean)
{
structInput->acc = 0;
structInput->prev_x = 0;
structInput->y = 0;
structInput->prev_y = 0;
}
}
/******************************************************************************
* IIR Single Pole High Pass - Fixed Version
* - use fixed integer math with internal variables of 32 bit
* - faster than float version without FPU
*
* - INPUT: iirHighPassFixed_t * inputStuct (pointer to struct with filter parameters)
* int32_t xValue (input/sample value)
*
* - RETURN: N/A (result returned inside the struct)
*
******************************************************************************
* - LIMITS: SHIFTS INPUT VALUE (Approximate value, influenced by cutoff value)
* 15 +/- 61.7k
* 14 +/-123.4k
* 13 +/-246.9k
* 12 +/-493.8k
* 11 +/-987.5k
* 10 +/-1.975M
* 9 +/-3.950M
* 8 +/-7.900M
*
* Reference: https://dspguru.com/dsp/tricks/fixed-point-dc-blocking-filter-with-noise-shaping/
******************************************************************************/
void iir_SinglePoleHighPass_Fixed(iirHighPassFixed_t * inputStuct, int32_t xValue)
{
inputStuct->acc -= inputStuct->prev_x;
inputStuct->prev_x = (xValue << inputStuct->shift_size);
inputStuct->acc += inputStuct->prev_x;
inputStuct->acc -= (inputStuct->A_param * inputStuct->prev_y);
inputStuct->prev_y = inputStuct->acc >> inputStuct->shift_size;
inputStuct->y = inputStuct->prev_y;
}
/******************************************************************************
* IIR Single Pole High Pass - Fixed Extended Version Initialization
*
* - INPUT: iirHighPassFixedExtended_t * structInput (pointer to struct with filter parameters)
* double cutoffFreq (pole value)
* uint_fast8_t shift (shift of fixed math - from 8 to 15)
* uint_fast8_t doClean (clean variables after change the pole)
*
* - RETURN: N/A (result returned inside the struct)
******************************************************************************/
void iir_SinglePoleHighPass_FixedExtended_Init(iirHighPassFixedExtended_t * structInput, double cutoffFreq, uint_fast8_t shift, uint_fast8_t doClean)
{
uint_fast8_t shift_temp = shift;
/*
* prevent shift bigger than 30 (filter stop to work)
*/
if (shift_temp > 30)
{
structInput->shift_size = 30;
}
else if(shift_temp < 8)
{
structInput->shift_size = 8;
}
else
{
structInput->shift_size = shift_temp;
}
structInput->cutoff_Freq = cutoffFreq;
structInput->A_param = (int32_t)( (1L << structInput->shift_size) * (structInput->cutoff_Freq) );
if (doClean)
{
structInput->acc = 0;
structInput->prev_x = 0;
structInput->y = 0;
structInput->prev_y = 0;
}
}
/******************************************************************************
* IIR Single Pole High Pass - Fixed Extended Version
* - use fixed integer math with internal variables of 64 bit
* - more headroom
*
* - INPUT: iirHighPassFixedExtended_t * inputStuct (pointer to struct with filter parameters)
* int32_t xValue (input/sample value)
*
* - RETURN: N/A (result returned inside the struct)
*
******************************************************************************
* - LIMITS: SHIFTS INPUT VALUE (Approximate value, influenced by cutoff)
* 30 +/- 61.7k
* 29 +/-123.4k
* 28 +/-246.9k
* 27 +/-493.8k
* 26 +/-987.5k
* 25 +/-1.975M
* 24 +/-3.950M
* 23 +/-7.900M
*
* Reference: https://dspguru.com/dsp/tricks/fixed-point-dc-blocking-filter-with-noise-shaping/
******************************************************************************/
void iir_SinglePoleHighPass_FixedExtended(iirHighPassFixedExtended_t * inputStuct, int32_t xValue)
{
inputStuct->acc -= inputStuct->prev_x;
inputStuct->prev_x = ((int64_t)xValue << inputStuct->shift_size);
inputStuct->acc += inputStuct->prev_x;
inputStuct->acc -= (inputStuct->A_param * (int64_t)inputStuct->prev_y);
inputStuct->prev_y = (int32_t)(inputStuct->acc >> inputStuct->shift_size);
inputStuct->y = inputStuct->prev_y;
}
/******************************************************************************
* IIR Single Pole Low Pass - Float Version - Initialization
*
* - INPUT: iirLowPassFloat_t * structInput (pointer to struct with filter parameters)
* float cutoffFreq (pole value)
* uint_fast8_t doClean (clean variables after change the pole)
*
* - RETURN: N/A (result returned inside the struct)
******************************************************************************/
void iir_SinglePoleLowPass_Float_Init(iirLowPassFloat_t * structInput, float cutoffFreq, uint_fast8_t doClean)
{
structInput->cutoff_Freq = cutoffFreq;
/***************************************
* b0 = (1.0f - x) input coeff
* a1 = x output coeff
***************************************/
structInput->b0 = (structInput->cutoff_Freq);
structInput->a1 = (1.0f - structInput->cutoff_Freq);
if (doClean)
{
structInput->prev_y = 0;
structInput->y = 0;
}
}
/******************************************************************************
* IIR Single Pole Low Pass - Float Version
* - use float math (more efficient with FPU)
*
* - INPUT: iirLowPassFloat_t * structInput (pointer to struct with filter parameters)
* float xValueFloat (input/sample value)
*
* - RETURN: N/A (result returned inside the struct)
*
* Reference: http://www.dspguide.com/ch19.htm
******************************************************************************/
void iir_SinglePoleLowPass_Float(iirLowPassFloat_t * inputStruct, float xValueFloat)
{
/***************************************
* b = input coefficients
* a = output coefficients
*
* y = b0*input + a1*y1
* y1 = y
***************************************/
inputStruct->y = (inputStruct->b0 * xValueFloat) + (inputStruct->a1 * inputStruct->prev_y);
inputStruct->prev_y = inputStruct->y;
}
/******************************************************************************
* IIR Single Pole Low Pass - Fixed Version - Initialization
*
* - INPUT: iirLowPassFixed_t * structInput (pointer to struct with filter parameters)
* float cutoffFreq (pole value)
* uint_fast8_t shift (shift of fixed math - from 8 to 15)
* uint_fast8_t doClean (clean variables after change the pole)
*
* - RETURN: N/A (result returned inside the struct)
******************************************************************************/
void iir_SinglePoleLowPass_Fixed_Init(iirLowPassFixed_t * structInput, float cutoffFreq, uint_fast8_t shift, uint_fast8_t doClean)
{
uint_fast8_t shift_temp = shift;
/**********************************************************
* prevent shift bigger than 12 (filter stop to work)
**********************************************************/
if (shift_temp > 12)
{
structInput->shift_size = 12;
}
else if(shift_temp < 8)
{
structInput->shift_size = 8;
}
else
{
structInput->shift_size = shift_temp;
}
structInput->RoundNumber = (1l << structInput->shift_size);
structInput->cutoff_Freq = cutoffFreq;
structInput->A_param = (int32_t)(structInput->cutoff_Freq * (1l << structInput->shift_size));
if (doClean)
{
structInput->SHIFTED_filtered = 0;
structInput->SHIFTED_last_filtered = 0;
structInput->y = 0;
}
}
/******************************************************************************
* IIR Single Pole Low Pass - Fixed Version
*
* - INPUT: iirLowPassFixed_t * structInput (pointer to struct with filter parameters)
* int32_t xValue (input/sample value)
*
* - RETURN: N/A (result returned inside the struct)
*
******************************************************************************
* - LIMITS: SHIFTS INPUT VALUE (Approximate value, influenced by cuttof)
* 12 +/- 30k
* 11 +/- 120k
* 10 +/- 480k
* 09 +/- 1.920M
* 08 +/- 7.680M
*
* Reference: https://learn.openenergymonitor.org/electricity-monitoring/ctac/digital-filters-for-offset-removal
******************************************************************************/
void iir_SinglePoleLowPass_Fixed(iirLowPassFixed_t * inputStruct, int32_t xValue)
{
/***************************************
* y = y1 + cutoffFreq * (input - y1)
* y1 = y
***************************************/
/* RoundNumber math adding 0.5 before return the number */
inputStruct->SHIFTED_filtered = inputStruct->SHIFTED_last_filtered + (inputStruct->A_param * ((xValue << inputStruct->shift_size) - inputStruct->SHIFTED_filtered + inputStruct->RoundNumber) >> inputStruct->shift_size);
inputStruct->SHIFTED_last_filtered = inputStruct->SHIFTED_filtered;
inputStruct->y = inputStruct->SHIFTED_filtered >> inputStruct->shift_size;
}
/******************************************************************************
* IIR Single Pole Low Pass - Fixed Extended Version - Initialization
*
* - INPUT: iirLowPassFixedExtended_t * structInput (pointer to struct with filter parameters)
* double cutoffFreq (pole value)
* uint_fast8_t shift (shift of fixed math - from 8 to 15)
* uint_fast8_t doClean (clean variables after change the pole)
*
* - RETURN: N/A (result returned inside the struct)
******************************************************************************/
void iir_SinglePoleLowPass_FixedExtended_Init(iirLowPassFixedExtended_t * structInput, double cutoffFreq, uint_fast8_t shift, uint_fast8_t doClean)
{
uint_fast8_t shift_temp = shift;
/*
* prevent shift bigger than 28 (filter stop to work)
*/
if (shift_temp > 28)
{
structInput->shift_size = 28;
}
else if(shift_temp < 8)
{
structInput->shift_size = 8;
}
else
{
structInput->shift_size = shift_temp;
}
structInput->RoundNumber = (int64_t)(1LL << structInput->shift_size);
structInput->cutoff_Freq = cutoffFreq;
structInput->A_param = (int64_t)(structInput->cutoff_Freq * (1LL << structInput->shift_size));
if (doClean)
{
structInput->SHIFTED_filtered = 0;
structInput->SHIFTED_last_filtered = 0;
structInput->y = 0;
}
}
/******************************************************************************
* IIR Single Pole Low Pass - Fixed Extended Version
*
* - INPUT: iirLowPassFixed_t * structInput (pointer to struct with filter parameters)
* int32_t xValue (input/sample value)
*
* - RETURN: N/A (result returned inside the struct)
*
* Reference: https://learn.openenergymonitor.org/electricity-monitoring/ctac/digital-filters-for-offset-removal
******************************************************************************/
void iir_SinglePoleLowPass_FixedExtended(iirLowPassFixedExtended_t * inputStruct, int32_t xValue)
{
/***************************************
* y = y1 + cutoffFreq * (input - y1)
* y1 = y
***************************************/
/* RoundNumber math adding 0.5 before return the number */
inputStruct->SHIFTED_filtered = inputStruct->SHIFTED_last_filtered + (inputStruct->A_param * (((int64_t)xValue << inputStruct->shift_size) - inputStruct->SHIFTED_filtered + inputStruct->RoundNumber) >> inputStruct->shift_size);
inputStruct->SHIFTED_last_filtered = inputStruct->SHIFTED_filtered;
inputStruct->y = (int32_t)(inputStruct->SHIFTED_filtered >> inputStruct->shift_size);
}
/******************************************************************************
* IIR Single Pole Low Pass - Fixed FAST Version - Initialization
*
* - INPUT: iirLowPassFixedExtended_t * structInput (pointer to struct with filter parameters)
* int_fast8_t attenuation (attenuation factor - see reference)
* uint_fast8_t doClean (clean variables after change the pole)
*
* - RETURN: N/A (result returned inside the struct)
******************************************************************************/
void iir_SinglePoleLowPass_Fixed_Fast_Init(iirLowPassFixedFast_t * structInput, int_fast8_t attenuation, int_fast8_t doClean)
{
structInput->attenuation = attenuation;
if (doClean)
{
structInput->filter_acc = 0;
structInput->y = 0;
}
}
/******************************************************************************
* IIR Single Pole Low Pass - Fixed FAST Version - Initialization
* - leaky integrator - very efficient
*
* - INPUT: iirLowPassFixedExtended_t * structInput (pointer to struct with filter parameters)
* int_fast8_t attenuation (attenuation factor - see reference)
* uint_fast8_t doClean (clean variables after change the pole)
*
* - RETURN: N/A (result returned inside the struct)
*
* Reference: http://www.edn.com/design/systems-design/4320010/A-simple-software-lowpass-filter-suits-embedded-system-applications
******************************************************************************/
void iir_SinglePoleLowPass_Fixed_Fast(iirLowPassFixedFast_t * inputStruct, int32_t xValue)
{
/*******************************************************
* filt = filt - (filt >> attenuationFactor) + input
* y = filt >> attenuationFactor
*******************************************************/
inputStruct->filter_acc = inputStruct->filter_acc - (inputStruct->filter_acc >> inputStruct->attenuation) + xValue;
inputStruct->y = (inputStruct->filter_acc >> inputStruct->attenuation);
}
/******************************************************************************
* Goertzel DFT - Float Array Version - Initialize Structure Parameters (FLOAT)
*
* - INPUT: goertzel_array_float_t * inputStruct (pointer to struct with parameters)
* float bin (desired bin - what harmonic)
* uint_fast16_t size_array (array size - number of samples)
*
* - RETURN: N/A (result returned inside the struct)
******************************************************************************/
void goertzelArrayInit_Float(goertzel_array_float_t * inputStruct, float bin, uint_fast16_t size_array)
{
float w = (2 * PI * bin)/size_array;
inputStruct->cr_float = cosf(w);
inputStruct->ci_float = sinf(w);
inputStruct->coeff_float = 2 * inputStruct->cr_float;
inputStruct->size_array = size_array;
}
/******************************************************************************
* Goertzel DFT - Float Math Array Version - Do the Math (FLOAT INPUT)
* - Calculate the amplitude of a desired bin (frequency) of a wave
*
* - INPUT: goertzel_array_float_t * inputStruct (pointer to struct with parameters)
* const float * arrayInput (pointer to array with input samples)
*
* - RETURN: N/A (result returned inside the struct)
******************************************************************************/
void goertzelArrayFloat_Float(goertzel_array_float_t * inputStruct, const float * arrayInput)
{
float s_float = 0;
float sprev_float = 0;
float sprev_float2 = 0;
uint_fast16_t size_array = inputStruct->size_array;
uint_fast16_t i;
for (i = 0; i < size_array ; i++)
{
s_float = arrayInput[i] + (inputStruct->coeff_float * sprev_float) - sprev_float2;
sprev_float2 = sprev_float;