Filter löschen
Filter löschen

Plotting frequency spectrum of a signal

2 Ansichten (letzte 30 Tage)
Ghazal Hnr
Ghazal Hnr am 28 Mär. 2017
Bearbeitet: Ghazal Hnr am 30 Mär. 2017
Hi, I wrote this code to obtain plot of frequency spectrum of my signals:
clc; clear all; close all;
%%Reading & plotting normal.wav file
normal = audioread('normal.wav');
figure(1)
plot(normal);
xlabel('Number of data','fontsize',12,'fontweight','b');
ylabel('Amplitude','fontsize',12,'fontweight','b');
title('Normal Heart Sound Signal','fontsize',14,'fontweight','b');
%%Reading & plotting murmur.wav file
murmur = audioread('murmur.wav');
figure(2)
plot(murmur);
xlabel('Number of data','fontsize',12,'fontweight','b');
ylabel('Amplitude','fontsize',12,'fontweight','b');
title('Murmur Heart Sound Signal','fontsize',14,'fontweight','b');
%%Fourier transform
fft_nor = fft(normal);
fft_mur = fft(murmur);
%%Calculating fs
% fs_normal
t_nor = 6; % s
N_nor = length(normal);
fs_nor = N_nor / t_nor; % Hz
% fs_murmur
t_mur = 8; % s
N_mur = length(murmur);
fs_mur = N_mur / t_mur; % Hz
%%Calculate the frequency axis
f_nor = fs_nor / 2 * linspace(-1,1,fs_nor);
f_mur = fs_mur / 2 * linspace(-1,1,fs_mur);
%%Plotting frequency spectrum
% Normal sound
figure(3)
plot(f_nor, abs(fft_nor));
xlabel('Frequency (Hz)','fontsize',12,'fontweight','b');
ylabel('Magnitude','fontsize',12,'fontweight','b');
title('Magnitude FFT Of Normal Signal','fontsize',14,'fontweight','b');
% Murmur sound
figure(4)
plot(f_mur, abs(fft_mur));
xlabel('Frequency (Hz)','fontsize',12,'fontweight','b');
ylabel('Magnitude','fontsize',12,'fontweight','b');
title('Magnitude FFT Of Murmur Signal','fontsize',14,'fontweight','b');
but after running it I have this error:
Error using plot Vectors must be the same lengths.
Error in Q5b (line 40) plot(f_nor, abs(fft_nor));
would anyone help me to fix it?

Akzeptierte Antwort

John BG
John BG am 28 Mär. 2017
Hi Ghazal
This is John BG ( <mailto:jgb2012@sky.com jgb2012@sky.com> ) I have alternative sound files and found the following to fix your script:
1.
one way to have your script running is to window fft_nor and fft_mur down to the lengths of f_nor and f_nur that happen to be shorter than the respective signals.
instead of
figure(3)
plot(f_nor, abs(fft_nor));
use
figure(3)
plot(f_nor, abs(fft_nor([1+numel(fft_nor)-numel(f_nor):end])));
or
plot(f_nor, abs(fft_nor([1:numel(f_nor)])));
and instead of
figure(4)
plot(f_mur, abs(fft_mur));
use
plot(f_mur, abs(fft_mur([1:numel(f_mur)])));
or
plot(f_mur, abs(fft_nor([1+numel(fft_mur)-numel(f_mur):end])));
2.
another way would be to first define the time parameters as well as the amount of frequencies you want on the spectrum, and then, not before, calculate the FFT, setting the amount of frequencies you want to the already defined frequencies, like this
..
t_nor = 6; % s
N_nor = length(nrl);
fs_nor = N_nor / t_nor; % Hz
% fs_murmur
t_mur = 8; % s
N_mur = length(murmur);
fs_mur = N_mur / t_mur; % Hz
%%Calculate the frequency axis
f_nor = fs_nor / 2 * linspace(-1,1,fs_nor);
f_mur = fs_mur / 2 * linspace(-1,1,fs_mur);
%%Fourier transform
fft_nor = fft(nrl,numel(f_nor));
fft_mur = fft(murmur,numel(f_mur));
%%Plotting frequency spectrum
% Normal sound
figure(3)
plot(f_nor, abs(fft_nor));
xlabel('Frequency (Hz)','fontsize',12,'fontweight','b');
ylabel('Magnitude','fontsize',12,'fontweight','b');
title('Magnitude FFT Of Normal Signal','fontsize',14,'fontweight','b');
% Murmur sound
figure(4)
plot(f_mur, abs(fft_mur));
xlabel('Frequency (Hz)','fontsize',12,'fontweight','b');
ylabel('Magnitude','fontsize',12,'fontweight','b');
title('Magnitude FFT Of Murmur Signal','fontsize',14,'fontweight','b');
3.
or you can also oversample: increase Fs on both signals until you get the same amount of frequencies that fft() assigns by default.
There are different ways to do this, but in this case it turns that if you sample faster by the following factors
6 and 8, signal and murmur respectively, see the K1 K2 factors I have introduced in the definitions of f_nor and f_mur
then your script also works ok, add something like this:
%%Fourier transform
fft_nor = fft(nrl);
fft_mur = fft(murmur);
%%Calculating fs
% fs_nrl
t_nor = 6; % s
N_nor = length(nrl);
fs_nor = N_nor / t_nor; % Hz
% fs_murmur
t_mur = 8; % s
N_mur = length(murmur);
fs_mur = N_mur / t_mur; % Hz
%%Calculate the frequency axis
K1=numel(fft_nor)/fs_nor
K2=numel(fft_mur)/fs_mur
f_nor = fs_nor / 2 * linspace(-1,1,fs_nor*K1);
f_mur = fs_mur / 2 * linspace(-1,1,fs_mur*K2);
%%Plotting frequency spectrum
% Normal sound
figure(3)
plot(f_nor, abs(fft_nor));
xlabel('Frequency (Hz)','fontsize',12,'fontweight','b');
ylabel('Magnitude','fontsize',12,'fontweight','b');
title('Magnitude FFT Of Normal Signal','fontsize',14,'fontweight','b');
% Murmur sound
figure(4)
plot(f_mur, abs(fft_mur));
xlabel('Frequency (Hz)','fontsize',12,'fontweight','b');
ylabel('Magnitude','fontsize',12,'fontweight','b');
title('Magnitude FFT Of Murmur Signal','fontsize',14,'fontweight','b');
if you find these lines useful would you please be so kind consider marking my answer as Accepted Answer?
To any other reader, if you find this answer of any help would you please click on the thumbs-up vote link,
thanks in advance for time and attention
John BG
  1 Kommentar
Ghazal Hnr
Ghazal Hnr am 28 Mär. 2017
Bearbeitet: Ghazal Hnr am 30 Mär. 2017
Thank you, my problem solved with your solution.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by