This is an automated email from the git hooks/post-receive script. yoh pushed a commit to annotated tag v0.1 in repository python-mne.
commit 722ad1f2ed0280948a4e21551e55c3408b26f1db Author: Alexandre Gramfort <[email protected]> Date: Tue May 10 15:44:33 2011 -0400 adding low and high pass filters --- mne/filter.py | 130 ++++++++++++++++++++++++++++++++++++++++++++++- mne/tests/test_filter.py | 12 +++++ 2 files changed, 141 insertions(+), 1 deletion(-) diff --git a/mne/filter.py b/mne/filter.py index a6a8be4..c2f4d73 100644 --- a/mne/filter.py +++ b/mne/filter.py @@ -43,6 +43,8 @@ def band_pass_filter(x, Fs, Fp1, Fp2): Fs1 = Fp1 - 0.5 in Hz Fs2 = Fp2 + 0.5 in Hz """ + Fp1 = float(Fp1) + Fp2 = float(Fp2) # Default values in Hz Fs1 = Fp1 - 0.5 @@ -53,7 +55,7 @@ def band_pass_filter(x, Fs, Fp1, Fp2): # Make x EVEN Norig = len(x) if Norig % 2 == 1: - x = np.c_[x, 1] + x = np.r_[x, 1] # Normalize frequencies Ns1 = Fs1 / (Fs / 2) @@ -74,3 +76,129 @@ def band_pass_filter(x, Fs, Fp1, Fp2): x = x[:Norig] return xf + + +def low_pass_filter(x, Fs, Fp): + """Lowpass filter for the signal x. + + An acausal fft algorithm is applied (i.e. no phase shift). The filter + functions is constructed from a Hamming window (window used in "firwin2" + function) to avoid ripples in the frequency reponse (windowing is a + smoothing in frequency domain) + + Parameters + ---------- + x : 1d array + Signal to filter + Fs : float + sampling rate + Fp : float + cut-off frequency + + Returns + ------- + xf : array + x filtered + + Notes + ----- + The passbands (Fp1 Fp2) frequencies are defined in Hz as + ------------------------- + | \ + | \ + | \ + | \ + | ----------------- + | + Fp Fp+0.5 + + """ + Fp = float(Fp) + + assert x.ndim == 1 + + # Make x EVEN + Norig = len(x) + if Norig % 2 == 1: + x = np.r_[x, 1] + + # Normalize frequencies + Ns = (Fp + 0.5) / (Fs / 2) + Np = Fp / (Fs / 2) + + # Construct the filter function H(f) + N = len(x) + + B = signal.firwin2(N, [0, Np, Ns, 1], [1, 1, 0, 0]) + + # Make zero-phase filter function + H = np.abs(fft(B)) + + xf = np.real(ifft(fft(x) * H)) + xf = xf[:Norig] + x = x[:Norig] + + return xf + + +def high_pass_filter(x, Fs, Fp): + """Highpass filter for the signal x. + + An acausal fft algorithm is applied (i.e. no phase shift). The filter + functions is constructed from a Hamming window (window used in "firwin2" + function) to avoid ripples in the frequency reponse (windowing is a + smoothing in frequency domain) + + Parameters + ---------- + x : 1d array + Signal to filter + Fs : float + sampling rate + Fp : float + cut-off frequency + + Returns + ------- + xf : array + x filtered + + Notes + ----- + The passbands (Fp1 Fp2) frequencies are defined in Hz as + ----------------------- + /| + / | + / | + / | + ---------- | + | + Fp-0.5 Fp + + """ + Fp = float(Fp) + + assert x.ndim == 1 + + # Make x ODD + Norig = len(x) + if Norig % 2 == 0: + x = np.r_[x, 1] + + # Normalize frequencies + Ns = (Fp - 0.5) / (Fs / 2) + Np = Fp / (Fs / 2) + + # Construct the filter function H(f) + N = len(x) + + B = signal.firwin2(N, [0, Ns, Np, 1], [0, 0, 1, 1]) + + # Make zero-phase filter function + H = np.abs(fft(B)) + + xf = np.real(ifft(fft(x) * H)) + xf = xf[:Norig] + x = x[:Norig] + + return xf diff --git a/mne/tests/test_filter.py b/mne/tests/test_filter.py new file mode 100644 index 0000000..98f5ff1 --- /dev/null +++ b/mne/tests/test_filter.py @@ -0,0 +1,12 @@ +import numpy as np +from numpy.testing import assert_array_almost_equal + +from ..filter import band_pass_filter, high_pass_filter, low_pass_filter + +def test_filters(): + a = np.random.randn(1000) + Fs = 1000 + bp = band_pass_filter(a, Fs, 4, 8) + lp = low_pass_filter(a, Fs, 8) + hp = high_pass_filter(lp, Fs, 4) + assert_array_almost_equal(hp, bp, 2) -- Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/python-mne.git _______________________________________________ debian-med-commit mailing list [email protected] http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/debian-med-commit
