Stuck with for loop with harmonic frequency algorithm

5 Ansichten (letzte 30 Tage)
Reece
Reece am 28 Okt. 2013
Kommentiert: A Jenkins am 28 Okt. 2013
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.

Akzeptierte Antwort

A Jenkins
A Jenkins am 28 Okt. 2013
for idx=1:length(A)
y(idx,:) = A(idx) * sin(linspace(0, nSeconds * Harm(idx) * 2* pi, nSeconds*Fs))
end
  2 Kommentare
Reece
Reece am 28 Okt. 2013
Hi, thank you.
Can you just explain whey idx works and not i ? Thanks
A Jenkins
A Jenkins am 28 Okt. 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."

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Audio I/O and Waveform Generation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by