matlab- amplitude/phase spectra and Fourier transform

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??
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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        

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hsuyfAuthor Commented:
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;
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 ?

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 ?

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Math / Science

From novice to tech pro — start learning today.