BTBts
BTBts

Reputation: 43

Read audio wav file and plot audio freq response smoothed in python

I am working with python and would like to perform the following. I have a wav audio file that I would like to read and plot frequency response for. I am only interested in the time window of 3-4 seconds, not the entire file. Also, I would like to resample my input file to 48k, instead of 192k which it comes in as.

I would like my plot to be with lines, of FFT length 8192, Hamming window, logx scale from 20 - 20k Hz.

Upvotes: 0

Views: 3154

Answers (2)

Frank Zalkow
Frank Zalkow

Reputation: 3930

Not hard to do in Python, you just have to install some packages:

import numpy as np
from scipy.io import wavfile
from scipy import signal
from matplotlib import pyplot as plt

sr, x = wavfile.read('file.wav')

x = signal.decimate(x, 4)
x = x[48000*3:48000*3+8192]
x *= np.hamming(8192)

X = abs(np.fft.rfft(x))
X_db = 20 * np.log10(X)
freqs = np.fft.rfftfreq(8192, 1/48000)
plt.plot(freqs, X_db)
plt.show()

What I do not understand, your time window of 3-4 seconds. Do you mean the window from 3 seconds on? (That is done in the code above.) Or do yo mean a window of 3 seconds duration? Then the window must be 3*48000 samples long.

Upvotes: 1

jaket
jaket

Reputation: 9341

Matlab is the easiest:

[x,fs] = audioread('file.wav');

;; downsample 4:1
x = resample(x, 4, 1);

;; snip 8192 samples 3 seconds in
x = x(48000*3:48000*3+8192);

plot(abs(fft(x));

I'll leave it to you to get the plot formatted the way you desire but just a hint is that you'll need to construct a frequency axis and snip the desired bins out of the fft.

Upvotes: 0

Related Questions