我编写了一个简单的 matlab / octave 函数来创建每个分量具有独立幅度、频率和相位的正弦曲线之和。有没有更干净的方法来写这个?
## Create a sum of cosines with independent amplitude, frequency and
## phase for each component:
## samples(t) = SUM(A[i] * sin(2 * pi * F[i] * t + Phi[i])
## Return samples as a column vector.
##
function signal = sum_of_cosines(A = [1.0],
F = [440],
Phi = [0.0],
duration = 1.0,
sampling_rate = 44100)
t = (0:1/sampling_rate:(duration-1/sampling_rate));
n = length(t);
signal = sum(repmat(A, n, 1) .* cos(2*pi*t' * F + repmat(Phi, n, 1)), 2);
endfunction
特别是,对 repmat() 的调用似乎有点笨拙——是否有一些漂亮的矢量化技术等着我学习?