我是matlab和信号处理的新手。我写了下面发布的代码。我无法理解的是,代码开头的time soecification
部分。我不知道为什么在指定间隔或持续时间时我们需要采样,我认为只需指定如下内容即可:
t = (0: 0.2: 1.0) for an example,
为什么我需要一些东西,如采样到静止信号等情节。
另一个问题是,这段代码给我一个错误,说paranthesis imbalance
如何解决它。
代码
%% 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}');
答案 0 :(得分:1)
因为我们正在处理数字化信号。您无法绘制无限量的信号样本。这就是为什么在处理数字化信号之前需要指定一些参数,例如采样频率。采样频率为您提供样本索引和时间之间的关系,即一秒信号中您有多少样本。
您也可以按照问题开头的建议定义时间向量,但如果您指定了采样信号的采样频率,则更方便。特别是如果你想做一些信号处理(见例如Nyquist sampling theorem),你必须意识到源于不同采样频率的限制和特性。
您的括号不平衡来自
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);
修改强>
要获得某种“实况情节”,您可以将绘图代码更改为以下内容:
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