Stuck with for loop with harmonic frequency algorithm
5 views (last 30 days)
Show older comments
Hello, I'm ultimately trying to configure a script to analyse the composition of waveforms in a signal generator and I'm strugling to find a way to do this neatly:
y1 = A(1) * sin(linspace(0, nSeconds * Harm(1) * 2* pi, nSeconds*Fs)); y2 = A(2) * sin(linspace(0, nSeconds * Harm(2) * 2* pi, nSeconds*Fs));
etc... Where A(#)=amplitude from another for loop Harm(#) = harmonic frequency from the other for loop.
I ideally want y(i) = A(i) * ....Harm(i).... but I'm unsure of how to do this with the sinusoid being an array of length nSeconds*Fs.
Any help greatly appreciated.
0 Comments
Accepted Answer
A Jenkins
on 28 Oct 2013
for idx=1:length(A)
y(idx,:) = A(idx) * sin(linspace(0, nSeconds * Harm(idx) * 2* pi, nSeconds*Fs))
end
2 Comments
A Jenkins
on 28 Oct 2013
I'm sure 'i' would work as well, but 'i' is also the imaginary number sqrt(-1), so it is bad practice to overwrite it with your counter variable, and confusing/dangerous to those of us who use it to mean sqrt(-1).
I would guess the problem you were having before is that you need the extra colon, y(idx,:) since your y is a vector. The extra colon says: "put the result in 'y', in the row 'idx', and use as many columns ':' as it takes."
More Answers (0)
See Also
Categories
Find more on Audio I/O and Waveform Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!