@@ -137,7 +137,76 @@ These two functions are identical to ``utils.from_int32_buffer``, and
137137``utils.from_uint32_buffer ``, with the exception that they convert
13813816-bit integers to floating point ``ndarray ``\ s.
139139
140+ spectrogram
141+ -----------
142+
143+ In addition to the Fourier transform and its inverse, ``ulab `` also
144+ sports a function called ``spectrogram ``, which returns the absolute
145+ value of the Fourier transform, also known as the power spectrum. This
146+ could be used to find the dominant spectral component in a time series.
147+ The arguments are treated in the same way as in ``fft ``, and ``ifft ``.
148+ This means that, if the firmware was compiled with complex support, the
149+ input can also be a complex array.
150+
151+ .. code ::
152+
153+ # code to be run in micropython
154+
155+ from ulab import numpy as np
156+ from ulab import utils as utils
157+
158+ x = np.linspace(0, 10, num=1024)
159+ y = np.sin(x)
160+
161+ a = utils.spectrogram(y)
162+
163+ print('original vector:\n', y)
164+ print('\nspectrum:\n', a)
165+
166+ .. parsed-literal ::
167+
168+ original vector:
169+ array([0.0, 0.009775015390171337, 0.01954909674625918, ..., -0.5275140569487312, -0.5357931822978732, -0.5440211108893697], dtype=float64)
170+
171+ spectrum:
172+ array([187.8635087634579, 315.3112063607119, 347.8814873399374, ..., 84.45888934298905, 347.8814873399374, 315.3112063607118], dtype=float64)
173+
174+
175+
176+
177+ As such, ``spectrogram `` is really just a shorthand for
178+ ``np.sqrt(a*a + b*b) ``, however, it saves significant amounts of RAM:
179+ the expression ``a*a + b*b `` has to allocate memory for ``a*a ``,
180+ ``b*b ``, and finally, their sum. In contrast, ``spectrogram `` calculates
181+ the spectrum internally, and stores it in the memory segment that was
182+ reserved for the real part of the Fourier transform.
183+
140184.. code ::
185+
186+ # code to be run in micropython
187+
188+ from ulab import numpy as np
189+ from ulab import utils as utils
190+
191+ x = np.linspace(0, 10, num=1024)
192+ y = np.sin(x)
193+
194+ a, b = np.fft.fft(y)
195+
196+ print('\nspectrum calculated the hard way:\n', np.sqrt(a*a + b*b))
197+
198+ a = utils.spectrogram(y)
199+
200+ print('\nspectrum calculated the lazy way:\n', a)
201+
202+ .. parsed-literal ::
141203
142- # code to be run in CPython
143204
205+ spectrum calculated the hard way:
206+ array([187.8635087634579, 315.3112063607119, 347.8814873399374, ..., 84.45888934298905, 347.8814873399374, 315.3112063607118], dtype=float64)
207+
208+ spectrum calculated the lazy way:
209+ array([187.8635087634579, 315.3112063607119, 347.8814873399374, ..., 84.45888934298905, 347.8814873399374, 315.3112063607118], dtype=float64)
210+
211+
212+
0 commit comments