我用Octave编写了以下代码:
1 %-----------------------------------------------------------------------------%
2 % INPUT SIGNAL %
3 %-----------------------------------------------------------------------------%
4
5 sampling_frequency = 8000;
6 sampling_period = 1/sampling_frequency;
7 samples_count = 8;
8 time_samples = sampling_period * (0 : samples_count-1);
9
10
11 amplitude_1 = 1;
12 frequency_1 = 1000;
13 phase_shift_1 = 0;
14 signal_samples_1 = amplitude_1*sin(2*pi * frequency_1 * ...
15 time_samples + phase_shift_1);
16
17
18 amplitude_2 = 0.5;
19 frequency_2 = 2000;
20 phase_shift_2 = 3*pi/4;
21 signal_samples_2 = amplitude_2*sin(2*pi * frequency_2 * ...
22 time_samples + phase_shift_2);
23
24 signal_samples = signal_samples_1 + signal_samples_2;
25 for samples_index = 0 : samples_count-1
26 fprintf(stdout, "Sample %d: ", samples_index)
27 endfor
28
正如你所看到的,在25
行,我想在单独的行打印每个样本的值,即
Sample <sample_number>: <sample_value>
我的问题是:如何索引数组的单独成员(例如input_signal
数组(?
我已经自己找到了解决方案,所以我想与您分享。
signal_samples = signal_samples_1 + signal_samples_2;
for samples_index = 1 : samples_count
fprintf(stdout, "Sample %d: %8.5fn", samples_index-1, ...
signal_samples(samples_index:samples_index))
endfor
输出:
Sample 0: 0.35355
Sample 1: 0.35355
Sample 2: 0.64645
Sample 3: 1.06066
Sample 4: 0.35355
Sample 5: -1.06066
Sample 6: -1.35355
Sample 7: -0.35355