@@ -24,7 +24,7 @@ using namespace MISCMATHS;
2424
2525
2626window::window ():PI(3.14159265359 ){
27-
27+ PassZero= 1 ;
2828 cutoff=0.0 ;
2929 SamplingRate=0.0 ;
3030 StopGain=0.0 ;
@@ -36,7 +36,7 @@ window::window():PI(3.14159265359){
3636
3737}
3838
39- window::window (float co, float sr, float sg, double tw):cutoff(co),SamplingRate(sr),StopGain(sg),TranWidth(tw),PI(3.14159265359 )
39+ window::window (float co, float sr, float sg, double tw, int pz ):cutoff(co),SamplingRate(sr),StopGain(sg),TranWidth(tw),PassZero(pz ),PI(3.14159265359 )
4040{
4141
4242 Nyq=SamplingRate/2.0 ;
@@ -47,6 +47,11 @@ window::window(float co, float sr, float sg, double tw):cutoff(co),SamplingRate(
4747}
4848
4949void window::kaiserord (float ripple, float width ) {
50+ // NiquistRate = SamplingRate/2.0
51+ // N, beta = sig.kaiserord(StopGain,TranWidth/NiquistRate)
52+ // taps = sig.firwin(N, CutOffFreq, window=('kaiser', beta), pass_zero=False, scale=True, nyq=NiquistRate)
53+ // return (taps,NiquistRate)
54+
5055
5156 float A = std::abs (ripple);
5257 if ( A < 8 ) {
@@ -56,6 +61,8 @@ void window::kaiserord (float ripple, float width ) {
5661 else {
5762 kaiser_beta (A);
5863 }
64+ std::cout<<" Raw N:" <<std::endl;
65+ std::cout<<(A-7.95 )/2.285 /(PI*width)+1 <<std::endl;
5966 N= (int ) std::ceil ((A-7.95 )/2.285 /(PI*width)+1 );
6067}
6168
@@ -71,7 +78,7 @@ void window::kaiser_beta (float a) {
7178 }
7279}
7380
74- float window::kaiser_atten ( int n, int width ){
81+ float window::kaiser_atten ( int n, float width ){
7582 float a;
7683 a=2.285 * (n - 1 ) * PI * width + 7.95 ;
7784 return a;
@@ -98,6 +105,7 @@ void window::print_info(){
98105 std::cout << " N=\t " <<N <<std::endl;
99106 std::cout << " beta=\t " <<beta <<std::endl;
100107 std::cout << bessi (0 ,2 ) <<std::endl;
108+
101109
102110}
103111
@@ -114,8 +122,28 @@ std::vector<float> window::get_fir(){
114122 float right;
115123 float scale_frequency;
116124 float s;
125+ // #int pass_nyquist;
126+
117127
118128 kaiserord (StopGain,TranWidth/Nyq); // given our StopGain and transition width, calculate the order (Sets N and Beta)
129+ bool odd=N&1 ;
130+ // #pass_nyquist=odd^PassZero;
131+
132+ std::cout<<N<<std::endl;
133+ std::cout<<odd<<std::endl;
134+
135+ if (PassZero==0 &&!odd)
136+ {
137+ N++;
138+ StopGain=kaiser_atten (N,TranWidth/Nyq);
139+ kaiser_beta (StopGain);
140+ // kaiserord(StopGain,TranWidth/Nyq);
141+ print_info ();
142+ std::cout<<" Recalculated N: " <<N<<std::endl;
143+ }
144+
145+ odd=N&1 ;
146+ // #pass_nyquist=odd^PassZero;
119147
120148 std::vector<float > m;
121149 m.reserve (N);
@@ -124,9 +152,20 @@ std::vector<float> window::get_fir(){
124152 std::vector<float > c;
125153 c.reserve (N);
126154
127-
128- cutoff_1d[0 ]=0.0 ;
129- cutoff_1d[1 ]=cutoff/Nyq;
155+
156+ if (PassZero==1 )
157+ {
158+ cutoff_1d[0 ]=0.0 ;
159+ cutoff_1d[1 ]=cutoff/Nyq;
160+ }
161+ else
162+ {
163+ cutoff_1d[0 ]=cutoff/Nyq;
164+ cutoff_1d[1 ]=1.0 ;
165+ std::cout<<" HPF mode" <<std::endl;
166+ }
167+
168+
130169
131170 alpha=0.5 *(N-1 );
132171
@@ -137,11 +176,7 @@ std::vector<float> window::get_fir(){
137176 h[i]=h[(int ) i]-cutoff_1d[0 ]*sincfn (cutoff_1d[0 ]*m[i]);
138177 }
139178
140- if (N % 2 != 0 )
141- {
142- N++;
143- alpha=0.5 *(N-1 );
144- }
179+
145180
146181 FIR.resize (N);
147182 left=cutoff_1d[0 ];
@@ -169,10 +204,15 @@ std::vector<float> window::get_fir(){
169204 s+=FIR[i]*c[i];
170205 }
171206
172- for (int i=0 ;i<N;i++)
173- {
174- FIR[i]=FIR[i]/s;
175- }
207+ // for (int i=0;i<N;i++)
208+ // {
209+ // FIR[i]=FIR[i]/s;
210+ // }
211+
212+ std::ofstream output_file (" /home/dparker/Desktop/FIRtest.txt" );
213+ output_file.precision (32 );
214+ std::ostream_iterator<float > output_iterator (output_file," \n " );
215+ std::copy (FIR.begin (),FIR.end (),output_iterator);
176216
177217
178218 return FIR;
0 commit comments