Clear Filters
Clear Filters

hello,please could anyone tell me how to graph magnitude and phase spectra for given a .mat file (fs=8 kHz)

2 views (last 30 days)
    clc
    clear all
    close all
    load a_sound.mat a
    x=a;
    y = fft(x);
    m = abs(y);
    p = angle(y);
    f = (0:length(y)-1)*50/length(y);
    subplot(2,1,1)
    plot(f,m)
    title('Magnitude')
    subplot(2,1,2)
    plot(f,rad2deg(p))
    title('Phase')

Accepted Answer

Star Strider
Star Strider on 15 Jan 2017
You have a few errors that are easily corrected. I suspect you were getting a ‘Vectors must be the same length’ error in your plot call.
Try this:
y = fft(x)/length(x);
m = abs(y);
p = angle(y);
f = linspace(0, 1, fix(length(y)/2)+1)*Fs/2 % ‘Fs’ Is Your Sampling Frequency
Iv = 1:length(f); % Index Vector
subplot(2,1,1)
plot(f,m(Iv)*2)
title('Magnitude')
grid
subplot(2,1,2)
plot(f,rad2deg(p(Iv)))
title('Phase')
grid
I don’t know what ‘sound.mat’ is, but remember that sound files are typically (Nx2), so bear in mind that there could be two columns, both of which would be processed together in your code. You may have to separate them in your code in that situation.
Please review the documentation for the fft (link) function, especially the code between the first (top) two plot figures.
  4 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!