why do we need time sampling to plot a stationary

2019-07-21 04:38发布

问题:

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}');

回答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