55 *
66 * The MIT License (MIT)
77 *
8- * Copyright (c) 2019-2021 Zoltán Vörös
8+ * Copyright (c) 2019-2024 Zoltán Vörös
99*/
1010
1111#include <math.h>
4545 imag[i] = data[2i+1]
4646
4747*/
48- void fft_kernel_complex (mp_float_t * data , size_t n , int isign ) {
48+ void fft_kernel (mp_float_t * data , size_t n , int isign ) {
4949 size_t j , m , mmax , istep ;
5050 mp_float_t tempr , tempi ;
5151 mp_float_t wtemp , wr , wpr , wpi , wi , theta ;
@@ -94,9 +94,9 @@ void fft_kernel_complex(mp_float_t *data, size_t n, int isign) {
9494/*
9595 * The following function is a helper interface to the python side.
9696 * It has been factored out from fft.c, so that the same argument parsing
97- * routine can be called from scipy.signal .spectrogram.
97+ * routine can be called from utils .spectrogram.
9898 */
99- mp_obj_t fft_fft_ifft_spectrogram (mp_obj_t data_in , uint8_t type ) {
99+ mp_obj_t fft_fft_ifft (mp_obj_t data_in , uint8_t type ) {
100100 if (!mp_obj_is_type (data_in , & ulab_ndarray_type )) {
101101 mp_raise_NotImplementedError (MP_ERROR_TEXT ("FFT is defined for ndarrays only" ));
102102 }
@@ -134,20 +134,10 @@ mp_obj_t fft_fft_ifft_spectrogram(mp_obj_t data_in, uint8_t type) {
134134 }
135135 data -= 2 * len ;
136136
137- if ((type == FFT_FFT ) || (type == FFT_SPECTROGRAM )) {
138- fft_kernel_complex (data , len , 1 );
139- if (type == FFT_SPECTROGRAM ) {
140- ndarray_obj_t * spectrum = ndarray_new_linear_array (len , NDARRAY_FLOAT );
141- mp_float_t * sarray = (mp_float_t * )spectrum -> array ;
142- for (size_t i = 0 ; i < len ; i ++ ) {
143- * sarray ++ = MICROPY_FLOAT_C_FUN (sqrt )(data [0 ] * data [0 ] + data [1 ] * data [1 ]);
144- data += 2 ;
145- }
146- m_del (mp_float_t , data , 2 * len );
147- return MP_OBJ_FROM_PTR (spectrum );
148- }
137+ if (type == FFT_FFT ) {
138+ fft_kernel (data , len , 1 );
149139 } else { // inverse transform
150- fft_kernel_complex (data , len , -1 );
140+ fft_kernel (data , len , -1 );
151141 // TODO: numpy accepts the norm keyword argument
152142 for (size_t i = 0 ; i < 2 * len ; i ++ ) {
153143 * data ++ /= len ;
@@ -202,7 +192,7 @@ void fft_kernel(mp_float_t *real, mp_float_t *imag, size_t n, int isign) {
202192 }
203193}
204194
205- mp_obj_t fft_fft_ifft_spectrogram (size_t n_args , mp_obj_t arg_re , mp_obj_t arg_im , uint8_t type ) {
195+ mp_obj_t fft_fft_ifft (size_t n_args , mp_obj_t arg_re , mp_obj_t arg_im , uint8_t type ) {
206196 if (!mp_obj_is_type (arg_re , & ulab_ndarray_type )) {
207197 mp_raise_NotImplementedError (MP_ERROR_TEXT ("FFT is defined for ndarrays only" ));
208198 }
@@ -258,15 +248,8 @@ mp_obj_t fft_fft_ifft_spectrogram(size_t n_args, mp_obj_t arg_re, mp_obj_t arg_i
258248 data_im -= len ;
259249 }
260250
261- if (( type == FFT_FFT ) || ( type == FFT_SPECTROGRAM ) ) {
251+ if (type == FFT_FFT ) {
262252 fft_kernel (data_re , data_im , len , 1 );
263- if (type == FFT_SPECTROGRAM ) {
264- for (size_t i = 0 ; i < len ; i ++ ) {
265- * data_re = MICROPY_FLOAT_C_FUN (sqrt )(* data_re * * data_re + * data_im * * data_im );
266- data_re ++ ;
267- data_im ++ ;
268- }
269- }
270253 } else { // inverse transform
271254 fft_kernel (data_re , data_im , len , -1 );
272255 // TODO: numpy accepts the norm keyword argument
@@ -275,13 +258,9 @@ mp_obj_t fft_fft_ifft_spectrogram(size_t n_args, mp_obj_t arg_re, mp_obj_t arg_i
275258 * data_im ++ /= len ;
276259 }
277260 }
278- if (type == FFT_SPECTROGRAM ) {
279- return MP_OBJ_FROM_PTR (out_re );
280- } else {
281- mp_obj_t tuple [2 ];
282- tuple [0 ] = MP_OBJ_FROM_PTR (out_re );
283- tuple [1 ] = MP_OBJ_FROM_PTR (out_im );
284- return mp_obj_new_tuple (2 , tuple );
285- }
261+ mp_obj_t tuple [2 ];
262+ tuple [0 ] = MP_OBJ_FROM_PTR (out_re );
263+ tuple [1 ] = MP_OBJ_FROM_PTR (out_im );
264+ return mp_obj_new_tuple (2 , tuple );
286265}
287266#endif /* ULAB_SUPPORTS_COMPLEX & ULAB_FFT_IS_NUMPY_COMPATIBLE */
0 commit comments