Skip to content

Commit 9e4fee0

Browse files
tkurakugrlee77
andauthored
added a frequency2scale function as the dual to scale2frequency (#635)
* added a frequency2scale function as the dual to scale2frequency Update test_functions.py documentation for frequency2scale function * add a bit more detail on frequency normalization to the docstring This info is present in doc/source/ref/cwt.rst, but it seems helpful to include it here as well. Co-authored-by: Gregory Lee <grlee77@gmail.com>
1 parent 5ce73c9 commit 9e4fee0

3 files changed

Lines changed: 56 additions & 2 deletions

File tree

doc/source/ref/cwt.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,26 @@ scales. The right column are the corresponding Fourier power spectra of each
147147
filter.. For scales 1 and 2 it can be seen that aliasing due to violation of
148148
the Nyquist limit occurs.
149149

150+
Converting frequency to scale for ``cwt``
151+
-------------------------------
152+
153+
To convert frequency to scale for use in the wavelet transform the function
154+
:func:`pywt.frequency2scale` can be used. This is the complement of the
155+
:func:`pywt.scale2frequency` function as seen in the previous section. Note that
156+
the input frequency in this function is normalized by 1/dt, or the sampling
157+
frequency fs. This function is useful for specifying the transform as a function
158+
of frequency directly.
159+
160+
.. sourcecode:: python
161+
162+
>>> import numpy as np
163+
>>> import pywt
164+
>>> dt = 0.01 # 100 Hz sampling
165+
>>> fs = 1 / dt
166+
>>> frequencies = np.array([100, 50, 33.33333333, 25]) / fs # normalize
167+
>>> scale = tfrequency2scale('cmor1.5-1.0', frequencies)
168+
>>> scale
169+
array([1., 2., 3., 4.])
170+
171+
150172
.. plot:: pyplots/cwt_scaling_demo.py

pywt/_functions.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
from ._extensions._pywt import DiscreteContinuousWavelet, Wavelet, ContinuousWavelet
1818

1919

20-
__all__ = ["integrate_wavelet", "central_frequency", "scale2frequency", "qmf",
20+
__all__ = ["integrate_wavelet", "central_frequency",
21+
"scale2frequency", "frequency2scale", "qmf",
2122
"orthogonal_filter_bank",
2223
"intwave", "centrfrq", "scal2frq", "orthfilt"]
2324

@@ -161,24 +162,48 @@ def central_frequency(wavelet, precision=8):
161162

162163

163164
def scale2frequency(wavelet, scale, precision=8):
164-
"""
165+
"""Convert from CWT "scale" to normalized frequency.
165166
166167
Parameters
167168
----------
168169
wavelet : Wavelet instance or str
169170
Wavelet to integrate. If a string, should be the name of a wavelet.
170171
scale : scalar
172+
The scale of the CWT.
171173
precision : int, optional
172174
Precision that will be used for wavelet function approximation computed
173175
with ``wavelet.wavefun(level=precision)``. Default is 8.
174176
175177
Returns
176178
-------
177179
freq : scalar
180+
Frequency normalized to the sampling frequency. In other words, for a
181+
sampling interval of `dt` seconds, the normalized frequency of 1.0
182+
corresponds to (`1/dt` Hz).
178183
179184
"""
180185
return central_frequency(wavelet, precision=precision) / scale
181186

187+
def frequency2scale(wavelet, freq, precision=8):
188+
"""Convert from to normalized frequency to CWT "scale".
189+
190+
Parameters
191+
----------
192+
wavelet : Wavelet instance or str
193+
Wavelet to integrate. If a string, should be the name of a wavelet.
194+
freq : scalar
195+
Frequency, normalized so that the sampling frequency corresponds to a
196+
value of 1.0.
197+
precision : int, optional
198+
Precision that will be used for wavelet function approximation computed
199+
with ``wavelet.wavefun(level=precision)``. Default is 8.
200+
201+
Returns
202+
-------
203+
scale : scalar
204+
205+
"""
206+
return central_frequency(wavelet, precision=precision) / freq
182207

183208
def qmf(filt):
184209
"""

pywt/tests/test_functions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ def test_scal2frq_scale():
2626
result = pywt.scale2frequency(w, scale, precision=12)
2727
assert_almost_equal(result, expected, decimal=3)
2828

29+
def test_frq2scal_freq():
30+
freq = 2
31+
w = pywt.Wavelet('db1')
32+
expected = 1. / freq
33+
result = pywt.frequency2scale(w, freq, precision=12)
34+
assert_almost_equal(result, expected, decimal=3)
35+
2936

3037
def test_intwave_orthogonal():
3138
w = pywt.Wavelet('db1')

0 commit comments

Comments
 (0)