matlab- amplitude/phase spectra and Fourier transform

Posted on 2006-04-26
Last Modified: 2016-02-10
I had two signal one is periodic and the other is aperiodic.for example

 period like--> waveformPeriodic = [t1,sin(t1/10)];
aperiodic like--> waveformAperiodic = [sort(rand(100,1)*100),rand(100,1)];

How can I generate the amplitude/phase spectra and Fourier transform for them??
Question by:hsuyf
    LVL 2

    Accepted Solution

    Hello hsuyf,

    Lets just take waveformPeriodic as our example.

    Save the code at the end of this message to a file called "fftden.m" somewhere on your matlab search path, then issue the following command to see an fft plot of your data :
    >> fftden(waveformPeriodic(:,1),waveformPeriodic(:,2));

    Issue the following slightly different command to return amplitude and phase spectra instead of generating a plot :
    >> [freq,density,phaseDeg] = fftden(waveformPeriodic(:,1),waveformPeriodic(:,2));



    function [freq,density,phaseDeg] = fftden(tIn,xIn,sampleFreq)
    % [freq,density,phaseDeg] = fftden(t,x,sampleFreq)
    % generate a power spectral density from a time domain signal using fft
    % density should scale with neither sampleFreq nor length of xIn
    % if no outputs are requested then a plot is generated
    % an example is given is the function is executed with no inputs

    if nargin>0
          if nargin < 3 || isempty(sampleFreq)
            sampleFreq = 1/min(diff(tIn));
          t       = min(tIn):1/sampleFreq:max(tIn);
          x       = interp1(tIn,xIn,t);
          n       = 2^(nextpow2(length(x)));    % use next highest power of 2 greater than or equal to length(x)
          fftOut  = fft(x,n);                   % take fft, padding with zeros so that length(fftOut) is equal to n
          fftMag   = fftOut.*conj(fftOut);
        warning off MATLAB:divideByZero
        fftPhase = 180*atan(imag(fftOut)./real(fftOut))./pi;
        warning on MATLAB:divideByZero
          i       = 1:(1+n/2);
          freq    = (i-1)*sampleFreq/n;          
          density = fftMag(i)/(n*sampleFreq);
          phaseDeg= fftPhase(i);
          if nargout==0
            plot(freq,density,'b', freq,density,'b.');
            xlabel('Frequency (Hz)');
            ylabel('Power Spectral Density');
            grid on
            zoom on        
        if nargin>0
            error('Can''t generate outputs without inputs.  Use "fftden;" to see example.')
        oi = 0.001;  %1024 2048 4096 8192
        t=0:1/1024:1;    [f,d]=fftden(t,sin(2*pi*t*200),1000);    c='b';   o=oi*0; plot(f,d+o,c, f,d+o,[c,'.']);    hold on
        t=0:1/1024:1;    [f,d]=fftden(t,sin(2*pi*t*200),2000);    c='g';   o=oi*1; plot(f,d+o,c, f,d+o,[c,'.']);
        t=0:1/1024:1;    [f,d]=fftden(t,sin(2*pi*t*200),4000);    c='r';   o=oi*2; plot(f,d+o,c, f,d+o,[c,'.']);
        t=0:1/1024:1;    [f,d]=fftden(t,sin(2*pi*t*200),8192);    c='m';   o=oi*3; plot(f,d+o,c, f,d+o,[c,'.']);
        t=0:1/1024:2;    [f,d]=fftden(t,sin(2*pi*t*200),8192);    c='k';   o=oi*4; plot(f,d+o,c, f,d+o,[c,'.']);
        t=0:1/1024:3;    [f,d]=fftden(t,sin(2*pi*t*200),8192);    c='c';   o=oi*5; plot(f,d+o,c, f,d+o,[c,'.']);
        t=0:1/1024:4;    [f,d]=fftden(t,sin(2*pi*t*200),8192);    c='k';   o=oi*6; plot(f,d+o,c, f,d+o,[c,'.']);
        t=0:1/1024:5;    [f,d]=fftden(t,sin(2*pi*t*200),8192);    c='c';   o=oi*7; plot(f,d+o,c, f,d+o,[c,'.']);

        title('Example set of Frequency Distributions');
        xlabel('Frequency (Hz)');
        ylabel('Power Spectral Density');
        grid on
        zoom on        

    Author Comment

    thanks Gordon
    Hi Gordon

      I think I will pick 2 wave file for my aperiodic signal and periodic signal. So the presentation would like below, how can I generate the amplitude spectra, phase spectra, and FT. since i am very new to matlab i think it looks like easier for me... could you please help me about this. thank you in advance...

    aperiodicwav = wavread('aperiod.wav');
    [aper, f] = wavread('aperiod.wav');
    periodicwav = wavread('period.wav');
    [per, fc] = wavread('period.wav');

    subplot(2,2,1), plot(aper), title('Entire waveform of aperiodic signal');
    smallRange = 100000:100000+floor(f/100);

    subplot(2,2,2), plot(per), title('Entire waveform of periodic signal');
    smallRange = 100000:100000+floor(fc/100);

    subplot(2,2,3), plot(?????????), title('phase spectra of aperiod signal');  -------> how can I plot "aperiod" signal'samplitude spectra, phase spectra, and FT here&#65311;

    subplot(2,2,4), plot(???????), title('phase spectra of aperiod signal');    -------->how can I plot "period" signal'samplitude spectra, phase spectra, and FT here&#65311;
    LVL 2

    Expert Comment

    Hello hsuyf,

    The answer I gave you already contains examples for how to achieve this.  

    If you need additional help please could you accept my answer and then ask a new question ?

    LVL 2

    Expert Comment

    Hello hsuyf,

    I need to earn more points to get to Premium Membership.

    If you think the answer I gave you was worth points (and hopefully you do because there's alot there) please could you accept it ?


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Suggested Solutions

    How to Win a Jar of Candy Corn: A Scientific Approach! I love mathematics. If you love mathematics also, you may enjoy this tip on how to use math to win your own jar of candy corn and to impress your friends. As I said, I love math, but I gu…
    Have you ever thought of installing a power system that generates solar electricity to power your house? Some may say yes, while others may tell me no. But have you noticed that people around you are now considering installing such systems in their …
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now