Stuck with for loop with harmonic frequency algorithm

조회 수: 5 (최근 30일)
Reece
Reece 2013년 10월 28일
댓글: A Jenkins 2013년 10월 28일
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.

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Audio I/O and Waveform Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by