99// Note:
1010// filter_gen.py provides C/C++ type functions which we have converted to TS
1111
12+ export class HighPassFilter {
13+ // State variables for the filter
14+ private z1 : number ;
15+ private z2 : number ;
16+ private x1 : number ;
17+ private currentSamplingRate : number ;
18+
19+ constructor ( ) {
20+ this . z1 = 0 ;
21+ this . z2 = 0 ;
22+ this . x1 = 0 ;
23+ this . currentSamplingRate = 0 ;
24+ }
25+
26+ // Set the sampling rate (250 or 500 Hz)
27+ setSamplingRate ( samplingRate : number ) : void {
28+ this . currentSamplingRate = samplingRate ;
29+ }
30+
31+ // Process the input sample through the appropriate high-pass filter
32+ process ( input : number ) : number {
33+ let output = input ;
34+
35+ switch ( this . currentSamplingRate ) {
36+ case 250 :
37+ // High-Pass Butterworth IIR digital filter for 250Hz
38+ // Sampling rate: 250.0 Hz, frequency: 1.0 Hz.
39+ {
40+ this . x1 = output - ( - 1.99644570 * this . z1 ) - ( 0.99645200 * this . z2 ) ;
41+ output = ( 0.99822443 * this . x1 ) + ( - 1.99644885 * this . z1 ) + ( 0.99822443 * this . z2 ) ;
42+ this . z2 = this . z1 ;
43+ this . z1 = this . x1 ;
44+ }
45+ break ;
46+
47+ case 500 :
48+ // High-Pass Butterworth IIR digital filter for 500Hz
49+ // Sampling rate: 500.0 Hz, frequency: 1.0 Hz.
50+ {
51+ this . x1 = output - ( - 1.99822285 * this . z1 ) - ( 0.99822443 * this . z2 ) ;
52+ output = ( 0.99911182 * this . x1 ) + ( - 1.99822364 * this . z1 ) + ( 0.99911182 * this . z2 ) ;
53+ this . z2 = this . z1 ;
54+ this . z1 = this . x1 ;
55+ }
56+ break ;
57+
58+ default :
59+ throw new Error ( `Unsupported sampling rate: ${ this . currentSamplingRate } . Only 250Hz and 500Hz are supported.` ) ;
60+ }
61+
62+ return output ;
63+ }
64+ }
65+
1266//Notch Filter 50Hz/60Hz
1367export class EXGFilter {
1468 // Properties to hold the state of the filter
@@ -21,7 +75,7 @@ export class EXGFilter {
2175 private bits : string | null ;
2276 private bitsPoints : number ;
2377 private yScale : number ;
24- private currentSamplingRate :number ;
78+ private currentSamplingRate : number ;
2579
2680
2781 constructor ( ) {
@@ -33,9 +87,9 @@ export class EXGFilter {
3387 this . x3 = 0 ;
3488 this . x4 = 0 ;
3589 this . bits = null ;
36- this . bitsPoints = 0 ;
37- this . yScale = 0 ;
38- this . currentSamplingRate = 0 ;
90+ this . bitsPoints = 0 ;
91+ this . yScale = 0 ;
92+ this . currentSamplingRate = 0 ;
3993 }
4094 //bits-
4195 //1.500
@@ -46,45 +100,45 @@ export class EXGFilter {
46100 //3.EEG
47101 //4.EMG
48102 // function to apply the
49- setbits ( bits : string , currentSamplingRate :number ) : void {
50- this . currentSamplingRate = currentSamplingRate ;
103+ setbits ( bits : string , currentSamplingRate : number ) : void {
104+ this . currentSamplingRate = currentSamplingRate ;
51105 this . bits = bits ;
52- this . bitsPoints = Math . pow ( 2 , parseInt ( bits )
106+ this . bitsPoints = Math . pow ( 2 , parseInt ( bits )
53107 ) ; // Adjust according to your ADC resolution
54108 this . yScale = 2 / this . bitsPoints ;
55109 }
56110
57111 process ( input : number , type : number ) : number {
58- if ( ! type ) return ( input - this . bitsPoints / 2 ) * this . yScale ;
112+ if ( ! type ) return input * this . yScale ;
59113 let output = input ;
60- let chData = 0 ;
114+ let chData = 0 ;
61115 switch ( this . currentSamplingRate ) {
62116 //bitsrate 500Hz
63- case 500 :
117+ case 500 :
64118 switch ( type ) {
65119 case 1 : // ECG Sampling rate: 500.0 Hz, frequency: 30.0 Hz.
66120 // Filter is order 2, implemented as second-order sections (biquads).
67121 this . x1 = output - ( - 1.47548044 * this . z1 ) - ( 0.58691951 * this . z2 ) ;
68122 output = 0.02785977 * this . x1 + 0.05571953 * this . z1 + 0.02785977 * this . z2 ;
69123 this . z2 = this . z1 ;
70124 this . z1 = this . x1 ;
71- chData = ( output - this . bitsPoints / 2 ) * this . yScale ;
125+ chData = output * this . yScale ;
72126 break ;
73127 case 2 : // EOG Sampling rate: 500.0 Hz, frequency: 10.0 Hz.
74128 // Filter is order 2, implemented as second-order sections (biquads).
75129 this . x2 = output - ( - 1.82269493 * this . z1 ) - ( 0.83718165 * this . z2 ) ;
76130 output = 0.00362168 * this . x2 + 0.00724336 * this . z1 + 0.00362168 * this . z2 ;
77131 this . z2 = this . z1 ;
78132 this . z1 = this . x2 ;
79- chData = ( output - this . bitsPoints / 2 ) * this . yScale ;
133+ chData = output * this . yScale ;
80134 break ;
81135 case 3 : // EEG Sampling rate: 500.0 Hz, frequency: 45.0 Hz.
82136 // Filter is order 2, implemented as second-order sections (biquads).
83137 this . x3 = output - ( - 0.51930341 * this . z1 ) - ( 0.21965398 * this . z2 ) ;
84138 output = 0.17508764 * this . x3 + 0.35017529 * this . z1 + 0.17508764 * this . z2 ;
85139 this . z2 = this . z1 ;
86140 this . z1 = this . x3 ;
87- chData = ( output - this . bitsPoints / 2 ) * this . yScale ;
141+ chData = output * this . yScale ;
88142 break ;
89143 case 4 : // EMG Sampling rate: 500.0 Hz, frequency: 70.0 Hz.
90144 // Filter is order 2, implemented as second-order sections (biquads).
@@ -107,7 +161,7 @@ export class EXGFilter {
107161 output = 0.09131490 * this . x1 + 0.18262980 * this . z1 + 0.09131490 * this . z2 ;
108162 this . z2 = this . z1 ;
109163 this . z1 = this . x1 ;
110- chData = ( output - this . bitsPoints / 2 ) * this . yScale ;
164+ chData = output * this . yScale ;
111165 break ;
112166
113167 case 2 : // EOG Sampling rate: 250.0 Hz, frequency: 10.0 Hz.
@@ -116,7 +170,7 @@ export class EXGFilter {
116170 output = 0.01335920 * this . x2 + 0.02671840 * this . z1 + 0.01335920 * this . z2 ;
117171 this . z2 = this . z1 ;
118172 this . z1 = this . x2 ;
119- chData = ( output - this . bitsPoints / 2 ) * this . yScale ;
173+ chData = output * this . yScale ;
120174 break ;
121175
122176 case 3 : // EEG Sampling rate: 250.0 Hz, frequency: 45.0 Hz.
@@ -125,7 +179,7 @@ export class EXGFilter {
125179 output = 0.17508764 * this . x3 + 0.35017529 * this . z1 + 0.17508764 * this . z2 ;
126180 this . z2 = this . z1 ;
127181 this . z1 = this . x3 ;
128- chData = ( output - this . bitsPoints / 2 ) * this . yScale ;
182+ chData = output * this . yScale ;
129183 break ;
130184
131185 case 4 : // EMG Sampling rate: 250.0 Hz, frequency: 70.0 Hz.
@@ -156,7 +210,7 @@ export class Notch {
156210 private z2_2 : number ;
157211 private x_1 : number ;
158212 private x_2 : number ;
159- private currentSamplingRate :number ;
213+ private currentSamplingRate : number ;
160214
161215
162216 constructor ( ) {
@@ -167,17 +221,17 @@ export class Notch {
167221 this . z2_2 = 0 ;
168222 this . x_1 = 0 ;
169223 this . x_2 = 0 ;
170- this . currentSamplingRate = 0 ;
224+ this . currentSamplingRate = 0 ;
171225
172226 }
173227
174- setbits ( currentSamplingRate :number ) : void {
175- this . currentSamplingRate = currentSamplingRate ;
228+ setbits ( currentSamplingRate : number ) : void {
229+ this . currentSamplingRate = currentSamplingRate ;
176230 }
177231
178232 // Method to apply the filter
179233 process ( input : number , type : number ) : number {
180- if ( ! type ) return input ;
234+ if ( ! type ) return input ;
181235 let output = input ;
182236 switch ( this . currentSamplingRate ) {
183237 case 500 : // 500Hz
0 commit comments