• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6228
  • Last Modified:

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??
0
hsuyf
Asked:
hsuyf
  • 3
1 Solution
 
gordonkennedyCommented:
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));

Gordon

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
 CUT AND PASTE THE FOLLOWING CODE INTO A FILE CALLED fftden.m
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

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));
      end
      
      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
        figure
        plot(freq,density,'b', freq,density,'b.');
        xlabel('Frequency (Hz)');
        ylabel('Power Spectral Density');
        grid on
        zoom on        
      end
else
    if nargin>0
        error('Can''t generate outputs without inputs.  Use "fftden;" to see example.')
    end
    figure
    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        
end
0
 
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;
0
 
gordonkennedyCommented:
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 ?

Thanks,
Gordon
0
 
gordonkennedyCommented:
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 ?

Thanks,
Gordon
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now