removehumnoise.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. function [z,y] = removehumnoise(x,sr)
  2. %-------------------------------------------------------------------
  3. % removal of recording electric noise with filters
  4. %-------------------------------------------------------------------
  5. % Input
  6. % x : data to denoise (one dimensional vector)
  7. % dt : time step of the data in [ms]
  8. % Output
  9. % z : filtered data (hum noise and high-frequency noise removed)
  10. % y : filtered data (hum noise removed)
  11. %-------------------------------------------------------------------
  12. % Technical notes
  13. % In Matlab, you normally have to take two steps to apply a filter to
  14. % data. In the first step, you construct a filter (fir1, cheby2, butter,
  15. % etc.), whose properties can be visualized with the 'freqz' function.
  16. % In the second step, you apply the filter to the data using the
  17. % 'filtfilt' function. In the code below, a bandstop filter is used
  18. % to remove the hum (50Hz) noise (output y). And then a lowpass filter
  19. % is additionally applied to remove high-frequency noise (output z).
  20. %-------------------------------------------------------------------
  21. % Go Ashida (go.ashida@uni-oldenburg.de) Mar 2020
  22. % modified by Bjarne Schultze (Dec 2021)
  23. %-------------------------------------------------------------------
  24. %% flag for plotting the results
  25. plotflag = 1; % 1: plot, 0: no plot
  26. %% time/frequency settings
  27. fs = sr;
  28. % 1000/dt; % [Hz] sampling rate
  29. nyq = fs/2; % Nyquist frequency (used for constructing a filter)
  30. %% defining filters
  31. % Here examples with the Chebyshev Type II filter are shown.
  32. % In Matlab, however, many more filters (such as FIR, IIR,
  33. % Butterworth, Bessel, etc.) are available. (see help)
  34. % filter settings
  35. bf1 = 50; % [Hz] center frequency of bandstop filter
  36. bw1 = 5; % [Hz] bandwidth of bandstop filter | adapdet by BS
  37. bdim = 2; % filter dimension
  38. bdb = 10; % [dB] attenuation level (10 db?)
  39. bf2 = 10; % [dB] cutoff frequency of lowpass filter
  40. % constructing cheby2 filter
  41. [b_bandstop, a_bandstop] = cheby2(bdim, bdb, [(bf1-bw1/2)/nyq, (bf1+bw1)/nyq], 'stop');
  42. [b_lowpass, a_lowpass] = cheby2(bdim, bdb, bf2/nyq, 'low'); % lowpass
  43. % showing filter properties - for more detail, see help for freqz
  44. %figure; freqz(b_bandstop, a_bandstop, 512, fs);
  45. %figure; freqz(b_lowpass, a_lowpass, 512, fs);
  46. %% applying filter
  47. y = filtfilt(b_bandstop, a_bandstop, x);
  48. z = filtfilt(b_lowpass, a_lowpass, y);
  49. %% plotting
  50. if(plotflag)
  51. tv = (0:length(x)-1)/sr; % time vector | adapted by BS
  52. figure; hold on;
  53. plot(tv,x,'r-');
  54. plot(tv,y,'b-');
  55. plot(tv,z,'g-');
  56. % labelling (added by BS)
  57. xlabel("time [s]"); ylabel("membrane potential [mV]");
  58. legend("original voltage trace", "hum noise filtered", ...
  59. "hum + high freq. noise filtered")
  60. end