Adding k discrete functions together in a "for" loop
显示 更早的评论
Hello,
I'm trying to create a loop that creates individual discrete sine waves from a vector of sampled frequencies (locs), then adds those values together to create a single combined waveform.
My code worked well symbolically:
syms tau
locs = [440 529 630] %example frequencies
combined = 0;
for k = 1:length(locs)
figure
hold on
wave(k) = sin(2*pi*locs(k)*tau); %generate sin wave for each sampled frequency
fplot(sin(2*pi*locs(k)*tau),[0 0.01])
hold off
combined = combined + wave(k); %add each generated sin wave to the previous one
end
figure
fplot(combined, [0 0.01])
But I am having trouble converting it to a discrete form. I get an error that says the indices on the left do not match the indices on the right, which I assume means that I am trying to put a 132300 element array (the sin function of t) into a 1x3 array (wave(k)).
t = 0:1/44100:3;
locs = [440 529 630] %example frequencies
combined = 0;
for k = 1:length(locs)
figure
hold on
wave(k) = sin(2*pi*locs(k)*t); %generate sin wave for each sampled frequency
stem(sin(2*pi*locs(k)*t),[0 441])
hold off
combined = combined + wave(k); %add each generated sin wave to the previous one
end
figure
stem(combined, [0 441])
Is there a better way to handle/store each of the generated sin waves so they can be added together in a discrete form?
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!