gpt4 book ai didi

matlab - 有没有一种更简洁的方法来生成正弦波总和?

转载 作者:行者123 更新时间:2023-12-02 04:48:47 28 4
gpt4 key购买 nike

我编写了一个简单的 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() 的调用似乎有点笨拙——是否有一些漂亮的矢量化技术等着我学习?

最佳答案

这是一样的吗?

signal = cos(2*pi*t' * F + repmat(Phi, n, 1)), 2) * A';

然后可能

signal = real(exp(j*2*pi*t'*F) * (A .* exp(j*Phi))');

如果你的内存有限,这应该能很好地工作:

e_jtheta = exp(j * 2 * pi * F / sampling_rate);
phasor = A .* exp(j*Phi);
samples = zeros(duration,1);
for k = 1:duration
samples(k) = real((e_jtheta .^ k) * phasor');
end

关于matlab - 有没有一种更简洁的方法来生成正弦波总和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19278769/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com