MATLAB Answers

How can I plot the frequency content of my sample?

53 views (last 30 days)
Jules Marrison
Jules Marrison on 10 Jan 2020
Commented: Meg Noah on 13 Jan 2020
Im trying to find the frequecny content of an audio signal and plot it. So far I have :
clear all %Clears the Workspace
close all %Closes all figue/plot windows opened by MATLAB
clc %Clears the command window
[Sample, Fs] = audioread('Snare_1.wav');
Sample = Sample(:,1);
period = 1/Fs;
t = 0:period:(length(Sample)*period)-period;
figure(1)
plot(t,Sample);
grid
xlabel('Seconds (s)');
ylabel('Amplitude');
xlim([0 0.8])
ylim([-0.1 0.1])
But I just cant figure out how to analyse the frequency content, please help if you can
https://we.tl/t-LlzN3QxitD : This is the snare file

Answers (1)

Meg Noah
Meg Noah on 11 Jan 2020
Did you find a solution yet? Here's one. I wasn't sure if you wanted the frequencies identified or just plotted.
clear all %Clears the Workspace
close all %Closes all figue/plot windows opened by MATLAB
clc %Clears the command window
[X, Fs] = audioread('Snare_1.wav');
X = X(:,1);
N = numel(X);
T = 1/Fs; % [s] sampling period
t = (0:N-1)*T; % [s] Time vector
deltaF = Fs/N; % [1/min]) frequency intervalue of discrete signal
figure(1)
subplot(2,1,1);
plot(t,X);
grid
xlabel('Seconds (s)');
ylabel('Amplitude');
xlim([0 0.8])
ylim([-0.1 0.1])
title('Amplitude of Wave');
% compute the fast fourier transform
Y = fft(X);
% manually shifting the FFT
Y = abs(Y/N);
Amp = [Y(ceil(end/2)+1:end)' Y(1) Y(2:ceil(end/2))']';
if (mod(N,2) == 0)
sampleIndex = -N/2:1:N/2-1; %raw index for FFT plot
else
sampleIndex = -(N-1)/2:1:(N-1)/2; %raw index for FFT plot
end
AmpSquared = Amp.^2;
subplot(2,1,2);
plot(deltaF*sampleIndex, AmpSquared);
hold on;
idx = find(AmpSquared > 15);
idx(sampleIndex(idx) < 0) = [];
plot(deltaF*sampleIndex(idx), AmpSquared(idx), '+');
xlabel('Frequency [Hz]');
ylabel('|FFT(Wave)|^2');
title('Power of FFT of Wave (Audible Range = 20-20\times10^4 Hz)');
xlim([20 2000]);
Snare.png
  4 Comments
Meg Noah
Meg Noah on 13 Jan 2020
It's a little complicated, because dB should have some sort of reference value to make them meaningful. I need to think about it a little more about whether or not .wav files have an implied reference that equates dB to energy in Watts or volume. There are some more complicated bandpass (frequency range) considerations for audio, because hearing ranges are limited.
Technically, this is 'dB' but audio community may have additional caveats. I'll think about it some more.
clear all %Clears the Workspace
close all %Closes all figue/plot windows opened by MATLAB
clc %Clears the command window
[X, Fs] = audioread('Snare_1.wav');
X = X(:,1);
N = numel(X);
T = 1/Fs; % [s] sampling period
t = (0:N-1)*T; % [s] Time vector
deltaF = Fs/N; % [1/min]) frequency intervalue of discrete signal
figure(1)
subplot(2,1,1);
plot(t,X);
grid
xlabel('Seconds (s)');
ylabel('Amplitude');
xlim([0 0.8])
ylim([-0.1 0.1])
title('Amplitude of Wave');
% compute the fast fourier transform
Y = fft(X);
% manually shifting the FFT
Y = abs(Y/N);
Amp = [Y(ceil(end/2)+1:end)' Y(1) Y(2:ceil(end/2))']';
if (mod(N,2) == 0)
sampleIndex = -N/2:1:N/2-1; %raw index for FFT plot
else
sampleIndex = -(N-1)/2:1:(N-1)/2; %raw index for FFT plot
end
subplot(2,1,2);
plot(deltaF*sampleIndex, 20*log10(Amp));
hold on;
idx = find(Amp.^2 > 15);
idx(sampleIndex(idx) < 0) = [];
plot(deltaF*sampleIndex(idx), 20*log10(Amp(idx)), '+');
xlabel('Frequency [Hz]');
ylabel('dB');
title('Power of FFT of Wave (Audible Range = 20-20\times10^4 Hz)');
xlim([0 2e4]);
It's basically some sort of noise, not pure tone content for this drum.
dB.png

Sign in to comment.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by