Files
Obsidian-Main/00. Inbox/使用 librosa 做 FFT.md

27 lines
648 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
```python
import numpy as np
n_fft=2048
ft = np.abs(librosa.stft(y[:n_fft], hop_length=n_fft+l))
plt.plot(ft)
plt.title('Spectrum')
plt.xlabel('Frequency Bin')
plt.ylabel('Amp1itude')
```
output e.g.:
![[Pasted image 20231212180917.png]]
這時候 Y 軸是 amplitude可以使用 `librosa.amplitude_to_db()` 來把 amplitude 轉為 db。
```python
import librosa.display
spec = np.abs(librosa.stft(y, hop_1ength=512))
spec = librosa.amplitude_to_db(spec, ref=np.max)
librosa.display.specshow(spec, sr=sr, x_axis='time', y_axis='log')
p1t.colorbar(format='%+2.0f dB')
plt.title( 'Spectrogram')
```
Output:
![[Pasted image 20231212181543.png]]