public void updateFilter(LTISystem [] newFilter) { // prevent updating coefficients while we're in the middle of a filter operation. Prevents index out of bound exceptions lock (filtLock) { bpFilter = newFilter; } }
public static LTISystem[] BandPass(double lofreq, double hifreq, int nSection) { if ((nSection % 2) == 1) nSection++; // force even # of sections Complex[] pol = Poles(hifreq - lofreq, nSection); LTISystem[] bpfilt = new LTISystem[nSection]; // translate the poles to band-pass position Complex[] bpol = new Complex[nSection]; double wlo = 2 * Math.PI * lofreq; double whi = 2 * Math.PI * hifreq; double ang = Math.Cos((whi + wlo) / 2) / Math.Cos((whi - wlo) / 2); for (int i = 0; i < nSection / 2; i++) { Complex p1 = new Complex(pol[i].Real + 1, pol[i].Imaginary); Complex tmp = Complex.Sqrt(p1 * p1 * ang * ang * 0.25 - pol[i]); bpol[2 * i] = (p1 * ang * 0.5) + tmp; bpol[2 * i + 1] = (p1 * ang * 0.5) - tmp; } // convert each conjugate pole pair to // difference equation for (int i=0;i<nSection;i++) { bpfilt[i] = new LTISystem(2,2); // put in conjugate pole pair bpfilt[i].b[1] = -2.0 * bpol[i].Real; bpfilt[i].b[2] = bpol[i].Real * bpol[i].Real + bpol[i].Imaginary * bpol[i].Imaginary; // put zeros at (1,0) and (-1,0) bpfilt[i].a[0] = 1.0; bpfilt[i].a[1] = 0.0; bpfilt[i].a[2] = -1.0; // normalise to unity gain double gain = (bpfilt[i].response((hifreq+lofreq)/2)).Magnitude; bpfilt[i].a[0] = bpfilt[i].a[0]/gain; // bpfilt[i].a[1] = bpfilt[i].a[1]/gain; // we know this is 0 since its a bandpass bpfilt[i].a[2] = bpfilt[i].a[2]/gain; } return bpfilt; }
private LTISystem notchPrototype(int harmonic) { LTISystem notch = new LTISystem(2, 2); double BW = 2 * Math.PI * bandwidth / samplingFrequency; double Wo = 2 * Math.PI * harmonic * notchFrequency / samplingFrequency; double Gb = Math.Pow(10,-aPass / 20); double beta = Math.Tan(BW/2)*Math.Sqrt(1 - Gb * Gb)/Gb; double gain = 1 / (1 + beta); notch.a[0] = gain; notch.a[1] = -2 * gain*Math.Cos(Wo); notch.a[2] = gain; notch.b[1] = notch.a[1]; notch.b[2] = 2 * gain - 1; return notch; }
private LTISystem[] notchesPrototype() { LTISystem[] notchFilt = new LTISystem[numHarmonics]; for (int i = 0; i < numHarmonics; ++i) { notchFilt[i] = notchPrototype(i); } return notchFilt; }