跳至主要內容

Octave 采样

Jia-Yin大约 2 分钟comm

我们可以用 Octave 写一个简单的程式,来观察单一频率弦波的采样问题。这边一样假设信号频率为9,然后我们分别用采样频率100和10来观察信号采样的结果。程式码如下:

% Define the time vector for one second duration
t = 0:0.001:1; % Time from 0 to 1 second with 1ms interval

% Define the frequency of the cosine wave
f = 9; % Frequency of 9 Hz

% Generate the cosine wave
cosine_wave = cos(2*pi*f*t);

% Plot the original cosine wave
figure; % Create a new figure
subplot(3,1,1);
plot(t, cosine_wave);
title('Original Cosine Wave (9 Hz)');

% Sample the cosine wave at 100 Hz
fs1 = 100; % Sampling rate of 100 Hz
n1 = 0:1/fs1:1; % Sample times
samples1 = cos(2*pi*f*n1);

% Plot the sampled signal at 100 Hz
subplot(3,1,2);
stem(n1, samples1);
title('Sampled at 100 Hz');

% Sample the cosine wave at 10 Hz
fs2 = 10; % Sampling rate of 10 Hz
n2 = 0:1/fs2:1; % Sample times
samples2 = cos(2*pi*f*n2);

% Plot the sampled signal at 10 Hz
subplot(3,1,3);
stem(n2, samples2);
title('Sampled at 10 Hz');

% Show the results
xlabel('Time (seconds)');
ylabel('Amplitude');

程式执行的结果如下:

Octave Sampling
Octave Sampling

观察执行的结果,可以发现采样速度100的时候,采样结果与原来的信号非常相似;但采样速度10的结果,则与原来的信号相距甚远。

練習 3

  1. 最下方的采样图形,看起来也很像一个弦波,它的频率看起来比较接近多少?为什么会有这样的结果?
  2. 到目前为止,所有谈到的信号都是单一频率,如果信号是两个频率合成的,那么采样的频率怎样才算恰当呢?譬如信号有两个频率分别为 9 和 20,修改以上的程式码,分别用不同的采样频率观察结果,并针对结果做一些说明。
  3. 如果信号是由更多不同频率成分所合成的,采样的速度应该如何设定较好?说明你的理由。