Add zero padded FFT calculation, could help #25
Zero pads the audio time samples until the length is equal to the next largest power of two. This improves the algorithmic complexity of the FFT calculations. Initialized the Hamming window when the module is loaded instead of every loop. Replaced a call to numpy.roll() with direct array index manipulation.
This commit is contained in:
parent
875943d6a1
commit
fba8175e99
@ -167,7 +167,7 @@ mel_smoothing = dsp.ExpFilter(np.tile(1e-1, config.N_FFT_BINS),
|
|||||||
alpha_decay=0.5, alpha_rise=0.99)
|
alpha_decay=0.5, alpha_rise=0.99)
|
||||||
volume = dsp.ExpFilter(config.MIN_VOLUME_THRESHOLD,
|
volume = dsp.ExpFilter(config.MIN_VOLUME_THRESHOLD,
|
||||||
alpha_decay=0.02, alpha_rise=0.02)
|
alpha_decay=0.02, alpha_rise=0.02)
|
||||||
|
fft_window = np.hamming(int(config.MIC_RATE / config.FPS) * config.N_ROLLING_HISTORY)
|
||||||
# Keeps track of the number of buffer overflows
|
# Keeps track of the number of buffer overflows
|
||||||
# Lots of buffer overflows could mean that FPS is set too high
|
# Lots of buffer overflows could mean that FPS is set too high
|
||||||
buffer_overflows = 1
|
buffer_overflows = 1
|
||||||
@ -186,7 +186,7 @@ def microphone_update(stream):
|
|||||||
# Normalize samples between 0 and 1
|
# Normalize samples between 0 and 1
|
||||||
y = y / 2.0**15
|
y = y / 2.0**15
|
||||||
# Construct a rolling window of audio samples
|
# Construct a rolling window of audio samples
|
||||||
y_roll = np.roll(y_roll, -1, axis=0)
|
y_roll[:-1] = y_roll[1:]
|
||||||
y_roll[-1, :] = np.copy(y)
|
y_roll[-1, :] = np.copy(y)
|
||||||
y_data = np.concatenate(y_roll, axis=0)
|
y_data = np.concatenate(y_roll, axis=0)
|
||||||
volume.update(np.nanmean(y_data ** 2))
|
volume.update(np.nanmean(y_data ** 2))
|
||||||
@ -197,10 +197,12 @@ def microphone_update(stream):
|
|||||||
led.update()
|
led.update()
|
||||||
else:
|
else:
|
||||||
# Transform audio input into the frequency domain
|
# Transform audio input into the frequency domain
|
||||||
XS, YS = dsp.fft(y_data, window=np.hamming)
|
N = len(y_data)
|
||||||
# Remove half of the FFT data because of symmetry
|
N_zeros = 2**int(np.ceil(np.log2(N))) - N
|
||||||
YS = YS[:len(YS) // 2]
|
# Pad with zeros until the next power of two
|
||||||
XS = XS[:len(XS) // 2]
|
y_data *= fft_window
|
||||||
|
y_padded = np.pad(y_data, (0, N_zeros), mode='constant')
|
||||||
|
YS = np.abs(np.fft.rfft(y_padded)[:N // 2])
|
||||||
# Construct a Mel filterbank from the FFT data
|
# Construct a Mel filterbank from the FFT data
|
||||||
mel = np.atleast_2d(np.abs(YS)).T * dsp.mel_y.T
|
mel = np.atleast_2d(np.abs(YS)).T * dsp.mel_y.T
|
||||||
# Scale data to values more suitable for visualization
|
# Scale data to values more suitable for visualization
|
||||||
|
Loading…
Reference in New Issue
Block a user