+ Post New Thread
Results 1 to 5 of 5
  1. #1
    Full Member level 3
    Points: 1,748, Level: 9

    Join Date
    Aug 2010
    Posts
    159
    Helped
    7 / 7
    Points
    1,748
    Level
    9

    simple matlab code displays different graph

    Hi,

    I don't understand why the following simple sine wave code displays different waveform when simulated repeatedly?

    fc=10;
    fs=2*fc;
    ts=1/fs;
    t=0:ts:50;
    x=sin(2*pi*fc*t);
    plot(t,x);

    the first run showed a correct sine wave, when i rerunned the code I get the following waveform-

    sinewave.bmp

    thanks

    •   AltAdvertisment

        
       

  2. #2
    Advanced Member level 4
    Points: 7,319, Level: 20
    Achievements:
    Created Blog entry 7 years registered
    xpress_embedo's Avatar
    Join Date
    Jul 2011
    Location
    India
    Posts
    1,162
    Helped
    199 / 199
    Points
    7,319
    Level
    20
    Blog Entries
    4

    Re: simple matlab code displays different graph

    See the amplitude its 10^-13 which you can assume to zero.

    Its negligible.
    What do you mean by rerun.

    - - - Updated - - -

    Have a look at this link:-
    https://sites.google.com/site/coolembeddedlaboratory/home/matlab

    Code:
    % This Script file Plots the Sine Wave with respect to time
    
    %The red text are comments
    
    clc; %Used to clear the screen (Just Clears the Screen)
    
    clear; %Used to clear the Variable Window (This clears the Memory)
    
    fSampling = 1000; %Sampling Frequency
    
    t = 0:1/fSampling:1; %Time Interval
    
    f = 3; %Carrier Frequency
    
    sine_wave = sin(2*pi*f*t);
    
    plot(t,sine_wave); %For Plotting the Sine Wave with Respect to time
    
    xlabel('Time Axis');
    
    ylabel('Sine Wave Amplitude');
    
    title('Sine Wave Plot');
    
    grid on;
    Try re-running this code.



    •   AltAdvertisment

        
       

  3. #3
    Full Member level 3
    Points: 1,748, Level: 9

    Join Date
    Aug 2010
    Posts
    159
    Helped
    7 / 7
    Points
    1,748
    Level
    9

    Re: simple matlab code displays different graph

    hi xpress, thanks but my code is fine, when the sampling frequency is increased the waveform is fine...

    I don't understand why the figure is like the one attached when I set the sampling frequency fs=2*fc that is twice the carrier???

    at fs=2fc the signal waveform is like an exponential increasing sinusoid.

    looking forward for answer



    •   AltAdvertisment

        
       

  4. #4
    Full Member level 5
    Points: 3,270, Level: 13
    Achievements:
    7 years registered

    Join Date
    Feb 2008
    Location
    Turkiye
    Posts
    259
    Helped
    60 / 60
    Points
    3,270
    Level
    13

    Re: simple matlab code displays different graph

    Since fs=2*fc in this case sampling timing (ts) occurs on time 2*pi*fc*(1/2fc), 2*pi*fc*(2/2fc), 2*pi*fc*(3/2fc) etc. at this moment ( pi, 2pi, 3pi,...) sin funciton is zero.
    So you see graph around zero. You need to increase the sampling freq.

    Another words, in this case you always sample sin wave at zero crossing points, so sampled values are zero.
    PS: But i don't understand why it is plotted correctly at the first run.



  5. #5
    Advanced Member level 4
    Points: 7,319, Level: 20
    Achievements:
    Created Blog entry 7 years registered
    xpress_embedo's Avatar
    Join Date
    Jul 2011
    Location
    India
    Posts
    1,162
    Helped
    199 / 199
    Points
    7,319
    Level
    20
    Blog Entries
    4

    Re: simple matlab code displays different graph

    Sampling Frequency >= 2* Signal Frequency

    Nyquist Criteria, so in order to reconstruct your signal, Sampling frequency must be 2 times greater than the Signal Frequency.
    The greater Sampling Frequency the better signal you will make but will also increase the bandwidth



--[[ ]]--