MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / calculate_fft

Function calculate_fft

machine_learning/mfcc.py:218–251  ·  view source on GitHub ↗

Calculate the Fast Fourier Transform (FFT) of windowed audio data. Args: audio_windowed: The windowed audio signal. ftt_size: The size of the FFT (default is 1024). Returns: The FFT of the audio data. Examples: >>> audio_windowed = np.array([[1.0, 2.0,

(audio_windowed: np.ndarray, ftt_size: int = 1024)

Source from the content-addressed store, hash-verified

216
217
218def calculate_fft(audio_windowed: np.ndarray, ftt_size: int = 1024) -> np.ndarray:
219 """
220 Calculate the Fast Fourier Transform (FFT) of windowed audio data.
221
222 Args:
223 audio_windowed: The windowed audio signal.
224 ftt_size: The size of the FFT (default is 1024).
225
226 Returns:
227 The FFT of the audio data.
228
229 Examples:
230 >>> audio_windowed = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
231 >>> audio_fft = calculate_fft(audio_windowed, ftt_size=4)
232 >>> bool(np.allclose(audio_fft[0], np.array([6.0+0.j, -1.5+0.8660254j,
233 ... -1.5-0.8660254j])))
234 True
235 """
236 # Transpose the audio data to have time in rows and channels in columns
237 audio_transposed = np.transpose(audio_windowed)
238
239 # Initialize an array to store the FFT results
240 audio_fft = np.empty(
241 (int(1 + ftt_size // 2), audio_transposed.shape[1]),
242 dtype=np.complex64,
243 order="F",
244 )
245
246 # Compute FFT for each channel
247 for n in range(audio_fft.shape[1]):
248 audio_fft[:, n] = fft.fft(audio_transposed[:, n], axis=0)[: audio_fft.shape[0]]
249
250 # Transpose the FFT results back to the original shape
251 return np.transpose(audio_fft)
252
253
254def calculate_signal_power(audio_fft: np.ndarray) -> np.ndarray:

Callers 1

mfccFunction · 0.85

Calls 2

transposeMethod · 0.80
emptyMethod · 0.45

Tested by

no test coverage detected