Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Harmonically Related Sinusoids

Subject: Harmonically Related Sinusoids

From: Lucas

Date: 9 Nov, 2010 09:39:03

Message: 1 of 6

Hey guys,

I'd like to generate various tones at harmonically related frequencies. Something like this:

i=1:50;
x(i)=sin(2*pi*F0*i*t);

where F0 is the fundamental frequency. I tried to accomplish that with a for loop but I get an error on the dimensions of the vectors.

Any help?

Thanks in advance.

Subject: Harmonically Related Sinusoids

From: PLH

Date: 9 Nov, 2010 10:07:04

Message: 2 of 6

I should probably just redirect you to another post, but since it looks like one of those rare occasions where this is something I can answer...

I'm presuming that F0 is a scalar and that t is a vector? If you have Nf=50 frequencies (Nf values of F0, essentially) at Nt times points (Nt values of t) then you need a NxM matrix to store the data. Here, x looks like a 1X50, which isn't going to hack it.

Try something like:
     tf = 1;dt = 0.01;
     t = 0:dt:tf;
     f = 1:50;

     [T,F] = meshgrid(t,f);
     X = sin(F.*T*2*pi);

     plot(t,X(6,:)) % plot 6-th harmonic

Then, the n-th row of X should be the n-th harmonic. Is that what you were after?

Subject: Harmonically Related Sinusoids

From: Steve Amphlett

Date: 9 Nov, 2010 11:18:03

Message: 3 of 6

"PLH " <paulhalkyard@googlemail.com> wrote in message <ibb6g8$7ii$1@fred.mathworks.com>...
> I should probably just redirect you to another post, but since it looks like one of those rare occasions where this is something I can answer...
>
> I'm presuming that F0 is a scalar and that t is a vector? If you have Nf=50 frequencies (Nf values of F0, essentially) at Nt times points (Nt values of t) then you need a NxM matrix to store the data. Here, x looks like a 1X50, which isn't going to hack it.
>
> Try something like:
> tf = 1;dt = 0.01;
> t = 0:dt:tf;
> f = 1:50;
>
> [T,F] = meshgrid(t,f);
> X = sin(F.*T*2*pi);
>
> plot(t,X(6,:)) % plot 6-th harmonic
>
> Then, the n-th row of X should be the n-th harmonic. Is that what you were after?

One slight nit to pick.

[T,F]=meshgrid(f,t)

will give column vectors for the harmonics. Column vectors are more natural for multi-channel processing in Matlab.

Subject: Harmonically Related Sinusoids

From: Greg Heath

Date: 9 Nov, 2010 11:37:56

Message: 4 of 6

On Nov 9, 4:39 am, "Lucas " <lm_arr...@hotmail.com> wrote:
> Hey guys,
>
> I'd like to generate various tones at harmonically related frequencies. Something like this:
>
> i=1:50;
> x(i)=sin(2*pi*F0*i*t);
>
> where F0 is the fundamental frequency. I tried to accomplish that with a for loop but I get an error on the dimensions of the vectors.
>
> Any help?
>
> Thanks in advance.

X = sin(2*pi*f(:)*t(:)');

Hope this helps.

Greg

Subject: Harmonically Related Sinusoids

From: Lucas

Date: 9 Nov, 2010 14:15:04

Message: 5 of 6

Thank you, guys! Now I can generate all the harmonics but my actual difficulty is to sum all of these harmonics. How can I sum all the columns?

Thank you.

Subject: Harmonically Related Sinusoids

From: Wayne King

Date: 9 Nov, 2010 15:23:03

Message: 6 of 6

"Lucas " <lm_arruda@hotmail.com> wrote in message <ibbl18$eqa$1@fred.mathworks.com>...
> Thank you, guys! Now I can generate all the harmonics but my actual difficulty is to sum all of these harmonics. How can I sum all the columns?
>
> Thank you.

Hi Lucas, Read the help for sum(). Once you have your data in a matrix, it is trivial to sum the matrix along any dimension.

Wayne

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us