Trying to Fourier transform an audio file

20 views (last 30 days)
Virginia Billings
Virginia Billings on 6 Nov 2021
Commented: Paul on 15 Nov 2023
I have the following MatLab code to fourier transform an audio file into frequency space, and then plot the power spectra with time, however, it is not working. Can someone help me correct it?
Note, the sampling rate is 44100 data points per second and the audio file is 4 seconds long.
f = audioread('beethoven.wav');
N = length(f);
tau = 4/N;
t = 0:tau:(N-1)*tau;
g = fft(f);
P = abs(g).^2;
dv = 1/(N*tau);
v = 0:dv:1/tau-dv;
plot(v,P)
  2 Comments
Walter Roberson
Walter Roberson on 6 Nov 2021
What difficulty are you encountering?
Note: at the moment it is not clear to me why your tau is 4/N instead of 2/N ?
Virginia Billings
Virginia Billings on 6 Nov 2021
The resulting plots are not in frequency space. They should have descreet peaks for each frequency in the audio file. I chose 4/N because when I plotted the time series for f v t, the t axis went to 4, suggesting my tau worked such that my time array was the correct length for it to end at 4 seconds, but that part was confusing me as well.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 6 Nov 2021
You should be using information about your sampling frequency to build time and frequency graphs.
They should have descreet peaks for each frequency in the audio file.
Ah? And what frequencies are those? Your filename is beethoven, suggesting that you have an extract from music. Not much music is made with pure sine waves that persist unchanged for the entire duration of the music.
Here's an example of the first two seconds of some work by Handel
%f = audioread('beethoven.wav');
load handel
num_secs = 2;
num_samps = num_secs*Fs;
f = y(1:num_samps,1);
N = length(f);
t = linspace(0, num_secs, num_samps);
plot(t, f);
g = fft(f);
P = abs(g).^2;
v = linspace(-Fs/2, Fs/2, num_samps);
plot(v, fftshift(P))
format long g
dx = 10^floor(log10(Fs/2));
xend = ceil(Fs/2/dx)*dx;
xst = -xend;
XT = xst:dx:xend;
xticks(XT)
title('power for all frequencies')
figure
plot(v, fftshift(P))
xlim([0 1500])
xticks(0:100:1500)
title('power to 1500')
You can see that some frequencies how more power than others, but that the frequencies are not discrete like you expected.
Please remember that music changes frequency over time. Please remember that piano and brass instruments are rich in harmonics. Please remember that the fft of a slope or the fft of a square wave involve an infinite number of frequencies...
  3 Comments
Walter Roberson
Walter Roberson on 15 Nov 2023
Fs is sampling frequency. In this particular case, it is being loaded when you load handel.
Fs is intended to convey "Frequency (that you sampled at)"
Paul
Paul on 15 Nov 2023
When num_samps is even, as in this case, the correct expression for v is
v = linspace(-Fs/2, Fs/2-Fs/num_samps, num_samps);
or
v = (-num_samps/2:num_samps/2-1)/num_samps*Fs;

Sign in to comment.

Tags

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!