[med-svn] [python-mne] 262/353: ENH : pass sfreq to stftfreq

Yaroslav Halchenko debian at onerussian.com
Fri Nov 27 17:25:11 UTC 2015


This is an automated email from the git hooks/post-receive script.

yoh pushed a commit to tag 0.4
in repository python-mne.

commit 37256a2800fe6fe5d4ea9937476da9da0421b7cf
Author: Alexandre Gramfort <alexandre.gramfort at inria.fr>
Date:   Thu Jul 12 20:13:24 2012 +0200

    ENH : pass sfreq to stftfreq
---
 mne/time_frequency/stft.py            | 11 +++++++++--
 mne/time_frequency/tests/test_stft.py | 11 ++++++++---
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/mne/time_frequency/stft.py b/mne/time_frequency/stft.py
index 15b72cf..65ad646 100644
--- a/mne/time_frequency/stft.py
+++ b/mne/time_frequency/stft.py
@@ -37,6 +37,9 @@ def stft(x, wsize, tstep=None, verbose=True):
     istft
     stftfreq
     """
+    if not np.isrealobj(x):
+        raise ValueError("x is not a real valued array")
+
     if x.ndim == 1:
         x = x[None, :]
 
@@ -86,7 +89,7 @@ def stft(x, wsize, tstep=None, verbose=True):
     for t in range(n_step):
         # Framing
         wwin = win / swin[t * tstep: t * tstep + wsize]
-        frame = np.conj(x[:, t * tstep: t * tstep + wsize]) * wwin[None, :]
+        frame = x[:, t * tstep: t * tstep + wsize] * wwin[None, :]
         # FFT
         fframe = fft(frame)
         X[:, :, t] = fframe[:, :n_freq]
@@ -176,13 +179,15 @@ def istft(X, tstep=None, Tx=None):
     return x
 
 
-def stftfreq(wsize):
+def stftfreq(wsize, sfreq=None):
     """Frequencies of stft transformation
 
     Parameters
     ----------
     wsize : int
         Size of stft window
+    sfreq : float
+        Sampling frequency. If None the frequencies are given in Hz.
 
     Returns
     -------
@@ -192,4 +197,6 @@ def stftfreq(wsize):
     n_freq = wsize / 2 + 1
     freqs = fftfreq(wsize)
     freqs = np.abs(freqs[:n_freq])
+    if sfreq is not None:
+        freqs *= float(sfreq)
     return freqs
diff --git a/mne/time_frequency/tests/test_stft.py b/mne/time_frequency/tests/test_stft.py
index 4f99933..11d08f3 100644
--- a/mne/time_frequency/tests/test_stft.py
+++ b/mne/time_frequency/tests/test_stft.py
@@ -8,19 +8,24 @@ from ..stft import stft, istft, stftfreq
 
 def test_stft():
     "Test stft and istft tight frame property"
+    sfreq = 1000.  # Hz
+    f = 7.  # Hz
     for T in [253, 256]:  # try with even and odd numbers
-        t = np.linspace(0, 20, T)
-        x = np.sin(30 * t)
+        t = np.arange(T).astype(np.float)
+        x = np.sin(2 * np.pi * f * t / sfreq)
         x = np.array([x, x + 1.])
         wsize = 128
         tstep = 4
         X = stft(x, wsize, tstep)
         xp = istft(X, tstep, Tx=T)
 
-        freqs = stftfreq(wsize)
+        freqs = stftfreq(wsize, sfreq=1000)
+
+        max_freq = freqs[np.argmax(np.sum(np.abs(X[0]) ** 2, axis=1))]
 
         assert_true(X.shape[1] == len(freqs))
         assert_true(np.all(freqs >= 0.))
+        assert_true(np.abs(max_freq - f) < 1.)
 
         assert_array_almost_equal(x, xp, decimal=6)
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/python-mne.git



More information about the debian-med-commit mailing list