Stuck with for loop with harmonic frequency algorithm

1 view (last 30 days)
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.

Accepted Answer

A Jenkins
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
Reece
Reece on 28 Oct 2013
Hi, thank you.
Can you just explain whey idx works and not i ? Thanks
A Jenkins
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."

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!