Matlab FFT

Discussion in 'Programmer's Corner' started by laura89, Oct 25, 2013.

  1. laura89

    Thread Starter New Member

    Oct 25, 2013
    4
    0
    Hello everyone, I'm implementing a matlab code that realizes the space vector modulation for a three-phase inverter. One of the points required is the representation of the spectrum of inverter output voltage.
    I wrote the following code:
    Code ( (Unknown Language)):
    1. function out=spettro(ValoreNelTempo,IntervalloTempo,fmax)
    2.  
    3. dime = max(size(ValoreNelTempo));
    4. f0 = 1/IntervalloTempo;
    5. n = floor(fmax/f0);
    6. if (n>dime)
    7.     n=dime-1;
    8. end
    9. ValoreNelTempo=reshape(ValoreNelTempo,dime,1);
    10. tempo = linspace(0,IntervalloTempo,length(ValoreNelTempo));
    11.  
    12. a=fft(ValoreNelTempo);
    13.  
    14. Armoniche=2*abs(a(2:dime))/dime;
    15. numarm=1:n;
    16.  
    17.  
    18.     figure(1), subplot(2,1,1)
    19.    
    20.     bar(numarm*f0,Armoniche(numarm),0.5,'r')
    21.     xlabel('frequenza - Hz'),grid
    22.    
    23.     figure(1), subplot(2,1,2)
    24.     plot(tempo,ValoreNelTempo,'b-');
    25.     xlabel('tempo - s'), grid
    26.    
    27. out=Armoniche(numarm);
    I later called up as follows:
    Code ( (Unknown Language)):
    1. tempo = simout.time;
    2. % tempo= t(time>0.3)
    3. y = simout.signals.values;
    4. % signal_mod= y(time>0.3)
    5. out = spettro(y,max(tempo),2/Ts);
    6.  
    7. figure(1),
    8. subplot(2,1,1), ylim([0 40]), xlim([0 100])
    9. legend('spettro del segnale campionato')
    10. subplot(2,1,2), ylim([-100 100]), xlim([0 2])
    11. legend('laura')
    Simout is a signal that I take from a scope of simulink.
    I get the attached graph.
    This chart is not correct, because I should do the FFT of the sine wave (omitting the transient) and then the teacher told me to take an integer number of periods.
    I am not able to do so. Could you please help me?
    Thanks in advance
    Ps: sorry for my english
     
  2. WBahn

    Moderator

    Mar 31, 2012
    17,788
    4,808
    What, exactly, is it that you need to do that you don't know how to do?

    It seems that you have a data vector that are a whole bunch of samples of a signal as a function of time. You then want to take a subset of that vector that starts far enough into the vector to get past the transient and that is the right length so as to amount to an integral number of periods of a particular frequency.

    What part of that process is tripping you up? Or it is some other aspect?
     
  3. laura89

    Thread Starter New Member

    Oct 25, 2013
    4
    0
    Thanks for the reply. The problem is that I'm not able to take only part of the signal. I tried to write in the code
    Code ( (Unknown Language)):
    1. signal_mod= y(time>0.3)
    , but still I get a wrong chart. Should I get only the fundamental harmonic. How can I fix the problem?
     
  4. WBahn

    Moderator

    Mar 31, 2012
    17,788
    4,808
    How is "time" encoded in any of these data structures at all? That's not al all apparent to me?

    What is "simout.time"?

    It's been some time since I worked with Matlab, so there are lots of things that you may need to describe to me.
     
  5. MrChips

    Moderator

    Oct 2, 2009
    12,453
    3,371
    I do both Matlab and FFT. If you can break it down to the heart of the problem I may be able to help. I cannot go over the entire code to search for your problem.
     
  6. laura89

    Thread Starter New Member

    Oct 25, 2013
    4
    0
    I try to explain the problem better.
    Simout cames from To Workspace block.
    The To Workspace block inputs a signal and writes the signal data to the MATLAB workspace. The block writes the data to an array or structure that has the name specified by the block's Variable name parameter. This format consists of a structure with three fields: time, signals, and blockName. The time field is empty. The blockName field contains the name of the To Workspace block. The signals field contains a structure with three fields: values, dimensions, and label.
    In my code the signal is the output voltage of an inverter. I need to find the FFT of this signal, but It has a transient that I must bypass.
    How can I do this? Can I write
    Code ( (Unknown Language)):
    1. signal_mod= y(time>0.3)
    ?
     
  7. laura89

    Thread Starter New Member

    Oct 25, 2013
    4
    0
    And why I have to use an integer number of periods?
     
Loading...