zerofilt.m 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. %ZEROFILT zero-phase bandpass filtering with firls.m and filtfilt.m
  2. % result = zerofilt(data,hp_freq,lp_freq,f_samp);
  3. % For HP filtering, fill lpfreq with Nyq.
  4. % For LP filtering, fill hpfreq with 0.
  5. % Data length must be bigger than 1200.
  6. % Sampling frequency fs is recommended to be smaller than 200 Hz.
  7. % Otherwise filtering performance decreases.
  8. function result = zerofilt(data,hpfreq,lpfreq,fs)
  9. Nyq = fs/2;
  10. if hpfreq == 0,
  11. % low-pass (when hpfreq = 0)
  12. f = [ 0 lpfreq lpfreq Nyq]/Nyq;
  13. a= [ 1 1 0 0 ];
  14. b = firls(500,f,a); % least-square linear phase FIR filter of order 400
  15. elseif lpfreq == Nyq,
  16. % high-pass (when lpfreq = Nyq);
  17. f = [ 0 hpfreq hpfreq Nyq]/Nyq;
  18. a= [ 0 0 1 1];
  19. b = firls(500,f,a); % least-square linear phase FIR filter of order 400
  20. else
  21. f = [ 0 hpfreq hpfreq lpfreq lpfreq Nyq]/Nyq;
  22. a= [ 0 0 1 1 0 0];
  23. b = firls(500,f,a); % least-square linear phase FIR filter of order 400
  24. end
  25. % test filter design
  26. % [H,f] = freqz(b,1,512,2);
  27. % figure; plot(f,abs(H));
  28. result = filtfilt(b,1,data); % zero-phase filtering