2

I am new to matlab and signal processing. I have wrote the below posted code. what i could not understand is, the time soecification section at the beginning of the code. I do not why do we need sampling when specifying an interval or time duration, i think it suffice to specify something like the following :

t = (0: 0.2: 1.0)  for an example,

why do i need some thing like sampling to plot such as stationary signal. another question is, this code gives me an error saying paranthesis imbalance how to solve it please.

Code

%% Time specifications:
  Fs = 8000;                       % samples per second
  dt = 1/Fs;                       % seconds per sample
  StopTime = 1;                    % seconds
  t = (0:dt:StopTime-dt);         % seconds

  x = (10)*cos(2*pi*3*t) ...          
  + (20)*cos(2*pi*6*t  ...  
   + (30)*cos(2*pi*10*t) ...
  + (50)*cos(2*pi*15*t);

  % Plot the signal versus time:
  figure;
  plot(t,x);
  xlabel('time (in seconds)');
  ylabel('Amplitude');
  title('Signal versus Time');
  title('{\bf Periodogram}');
Amrmsmb
  • 1
  • 27
  • 104
  • 226

1 Answers1

1

Because we are dealing with digitized signals. You can not plot an infinite amount of samples of your signal. That is why you need to specify some parameters prior to working with digitized signals, such as the sampling frequency. The sampling frequency gives you a relationship between your samples indices and time, namely how many samples you have in one second of signal.

You can also define your time vectors as you suggested in the beginning of your question, but it is way more convenient, if you have specified a sampling frequency with which your signal was sampled. Especially if you want to do some signal processing (see e.g.: Nyquist sampling theorem) it is crucial that you are aware of limitations and properties stemming from different sampling frequencies.

Your parenthesis imbalance is coming from

  x = (10)*cos(2*pi*3*t) ...          
  + (20)*cos(2*pi*6*t)  ...  % <= Missing parenthesis in this line
   + (30)*cos(2*pi*10*t) ...
  + (50)*cos(2*pi*15*t);

EDIT:

To get kind of a "live plot" you can change your plotting code to something like this:

figure;
xlabel('time (in seconds)');
ylabel('Amplitude');
title('Signal versus Time');

h = plot(nan);
for i=1:length(t)
    set(h,'YData', x(1:i), 'XData', t(1:i));
    drawnow
end

EDIT2:

% Time specifications:
Fs = 8000;                       % samples per second
dt = 1/Fs;                       % seconds per sample
StopTime = 1;                    % seconds
t = (0:dt:StopTime-dt);         % seconds

x1 = (10)*cos(2*pi*3*t);
x2 = (20)*cos(2*pi*6*t);
x3 = (30)*cos(2*pi*10*t);
x4 = (50)*cos(2*pi*15*t);

% Plot the signal versus time:
figure;
xlabel('time (in seconds)');
ylabel('Amplitude');
title('Signal versus Time');

h1 = plot(nan, 'r');
hold on
h2 = plot(nan, 'g');
hold on
h3 = plot(nan, 'black');
hold on
h4 = plot(nan, 'b');
hold on
for i=1:length(t)
    set(h1,'YData', x1(1:i), 'XData', t(1:i));
    set(h2,'YData', x2(1:i), 'XData', t(1:i)); 
    set(h3,'YData', x3(1:i), 'XData', t(1:i)); 
    set(h4,'YData', x4(1:i), 'XData', t(1:i)); 
    drawnow
end
lmNt
  • 3,822
  • 1
  • 16
  • 22
  • thank you for the answer. would you please tell me how to convert the mentioned code into a non-stationary signal the one that varies with time? what i attempted is, the same code in YOUR answer but each time "t" is replaced with t-.2, t-.7, t-.5 respectively, is that is it? – Amrmsmb Dec 28 '14 at 13:40
  • I think you have some wrong impressions of stationary signals (which is kind of a more statistical term) and their visualization via plotting. What you have is essentially a view of the signal in a larger time "frame". Meaning you see your time proceeding on the x-axis and your appropriate instantaneous value of x. If you want to really see a progression of the plot you would need some kind of animation. But this won't give you any additional information. – lmNt Dec 28 '14 at 13:52
  • I added some code to my answer which will give you a live plot. Is that what you wanted? – lmNt Dec 28 '14 at 14:04
  • of course it is a nice way to present a signal, i have not even thought that matlab has such functionality. but, i think it does not show a non stationary signal which its frequency contents varies with the time – Amrmsmb Dec 28 '14 at 14:16
  • You really have to clarify what exactly you want to achieve. The ultimate goal of you would be a Fourier analysis which will give you the frequency components. But if you want to see the single contents (meaning all the single cosine waves in your additive signal) varying over time, then see my second edit. Because all your cosine waves have a distinct frequency, there will be no change in the frequency components over time. At any instance of your signal you will have your frequency components at 3, 6, 10 and 15 Hz. – lmNt Dec 28 '14 at 14:31
  • can you please tell me why in this equ. "t = (0:dt:StopTime-dt);" we uses "stoptime-dt" and not only stoptime – Amrmsmb Dec 28 '14 at 15:16
  • Because we start at `t=0` and we want one second of data (namely 8000 samples). If we would not use `stoptime-dt` we would have 8001 samples in our `t` vector. – lmNt Dec 28 '14 at 16:13
  • maybe you want to answer this as well :) http://stackoverflow.com/questions/27678678/why-there-is-discontinuity-between-the-signals-and-the-specified-frequencies-do – Amrmsmb Dec 28 '14 at 17:11