Discussion:

How to get magnitude and phase onfo of a given signal

(too old to reply)

Ashwini Deshpande

2009-07-06 10:12:01 UTC

Permalink

Hi,

I have sine wave as follows,

t=0:0.01:1;

x=sin(2*pi*4*t);

if i want to know the magnitude and phase of this signal, what do i do ??

It is something like,

[magnitude, phase] = function(x);

Can anyone tell me that, is there any such library function available in matlab which gives me magnitude and phase information of signal given.

Thanks !

Ashwini

Wayne King

2009-07-06 11:18:01 UTC

Permalink

hi Ashwini, please look at the help for fft

Post by Ashwini Deshpande

doc fft

Hope that helps,

wayne

Post by Ashwini Deshpande

Hi,

I have sine wave as follows,

t=0:0.01:1;

x=sin(2*pi*4*t);

if i want to know the magnitude and phase of this signal, what do i do ??

It is something like,

[magnitude, phase] = function(x);

Can anyone tell me that, is there any such library function available in matlab which gives me magnitude and phase information of signal given.

Thanks !

Ashwini

Dave Robinson

2009-07-06 11:23:01 UTC

Permalink

Post by Ashwini Deshpande

Hi,

I have sine wave as follows,

t=0:0.01:1;

x=sin(2*pi*4*t);

if i want to know the magnitude and phase of this signal, what do i do ??

It is something like,

[magnitude, phase] = function(x);

Can anyone tell me that, is there any such library function available in matlab which gives me magnitude and phase information of signal given.

Thanks !

Ashwini

If you know what the frequency of your signal is, as you do in your example, then create a unity amplitude sine wave and a unity amplitude cosine wave of exactly the same frequency but of zero phase. Now take your signal and do a sample by sample multiplication first by the cosine wave you have just synthesized, then accumulate the results - repeat this process for your sine wave. You should get 2 simple numbers out of each multiply accumulate operation. If you do a pythagorean sum on these numbers, it will tell you the amplitude of your signal, and if you take ATAN2 on the Cosine accumulation and the Sine accumulation, this will give you a measure of the phase.

If you don't know the frequency of your signal, then you probably need to resort to using the FFT, if so come back.

Regards

Dave Robinson

Ashwini Deshpande

2009-07-06 12:21:01 UTC

Permalink

Post by Dave Robinson

Post by Ashwini Deshpande

Hi,

I have sine wave as follows,

t=0:0.01:1;

x=sin(2*pi*4*t);

if i want to know the magnitude and phase of this signal, what do i do ??

It is something like,

[magnitude, phase] = function(x);

Can anyone tell me that, is there any such library function available in matlab which gives me magnitude and phase information of signal given.

Thanks !

Ashwini

If you know what the frequency of your signal is, as you do in your example, then create a unity amplitude sine wave and a unity amplitude cosine wave of exactly the same frequency but of zero phase. Now take your signal and do a sample by sample multiplication first by the cosine wave you have just synthesized, then accumulate the results - repeat this process for your sine wave. You should get 2 simple numbers out of each multiply accumulate operation. If you do a pythagorean sum on these numbers, it will tell you the amplitude of your signal, and if you take ATAN2 on the Cosine accumulation and the Sine accumulation, this will give you a measure of the phase.

If you don't know the frequency of your signal, then you probably need to resort to using the FFT, if so come back.

Regards

Dave Robinson

Thanks for ur reply,

can plz clear me that what do u mean by accumulating result. Is it same as calculating the mean.

Thanks !

Ashwini

Dave Robinson

2009-07-06 16:29:03 UTC

Permalink

Post by Ashwini Deshpande

Thanks for ur reply,

can plz clear me that what do u mean by accumulating result. Is it same as calculating the mean.

Thanks !

Ashwini

When you do the sample by sample multiplication (.*) of the two waveforms, you will end up with a vector of the same length as your (Cos)Sine wave and signal. By accumulate I meant add up all the elements in that vector - yes you are right, it is directly proportional to the mean value of that vector.

Regards

Dave Robinson

TideMan

2009-07-06 20:19:55 UTC

Permalink

Post by Dave Robinson

Post by Ashwini Deshpande

Thanks for ur reply,

can plz clear me that what do u mean by accumulating result. Is it same as calculating the mean.

Thanks !

Ashwini

When you do the sample by sample multiplication (.*) of the two waveforms, you will end up with a vector of the same length as your (Cos)Sine wave and signal. By accumulate I meant add up all the elements in that vector - yes you are right, it is directly proportional to the mean value of that vector.

Regards

Dave Robinson

Another way of doing what Dave suggests is using Matlab's \ facility:

wt=2*pi*4*t;

coef=[cos(wt) sin(wt)]\x;

amp=sqrt(coef(1)^2 + coef(2)^2);

phi=atan2(coef(1),coef(2))*180/pi;

In principle, this is the way tidal constituents are calculated from a

tide gauge record, except that there are not one, but up to 600

frequencies involved.

Ashwini Deshpande

2009-07-07 04:49:02 UTC

Permalink

Post by TideMan

Post by Dave Robinson

When you do the sample by sample multiplication (.*) of the two waveforms, you will end up with a vector of the same length as your (Cos)Sine wave and signal. By accumulate I meant add up all the elements in that vector - yes you are right, it is directly proportional to the mean value of that vector.

Regards

Dave Robinson

wt=2*pi*4*t;

coef=[cos(wt) sin(wt)]\x;

amp=sqrt(coef(1)^2 + coef(2)^2);

phi=atan2(coef(1),coef(2))*180/pi;

In principle, this is the way tidal constituents are calculated from a

tide gauge record, except that there are not one, but up to 600

frequencies involved.

Thanks a lot for ur reply,

My code is as follows:

t=0:0.0001:0.02;

ph=0;

x1=sin(2*pi*100*t);

x2=sin(2*pi*100*t+ph);

plot(t,x1,t,x2);

X1_sq=x1.^2;

X2_sq=x2.^2;

s1=mean(X1_sq);

s2=mean(X2_sq);

amp=sqrt(s1^2+s2^2);

phi=atan2(s2,s1)*180/pi;

when i run this code i get phi value around 45 for any value of ph.

But i suppose to get 0 for ph=0 and so on..... what is wrong in this .. ??

Thanks !

Ashwini

Srikanth

2012-04-24 18:41:07 UTC

Permalink

Post by TideMan

Post by Dave Robinson

Post by Ashwini Deshpande

Thanks for ur reply,

can plz clear me that what do u mean by accumulating result. Is it same as calculating the mean.

Thanks !

Ashwini

When you do the sample by sample multiplication (.*) of the two waveforms, you will end up with a vector of the same length as your (Cos)Sine wave and signal. By accumulate I meant add up all the elements in that vector - yes you are right, it is directly proportional to the mean value of that vector.

Regards

Dave Robinson

wt=2*pi*4*t;

coef=[cos(wt) sin(wt)]\x;

amp=sqrt(coef(1)^2 + coef(2)^2);

phi=atan2(coef(1),coef(2))*180/pi;

In principle, this is the way tidal constituents are calculated from a

tide gauge record, except that there are not one, but up to 600

frequencies involved.

Hello,

I saw your comment and wanted to know if you could provide some insight on how to apply this method to a signal that has more than 1 frequency involved (in my case 3). Will this method provide a good result if the estimate of the frequency w is slightly off (due to experimental error)?

Thanks in advance for your help

-Sri

Ihaveideas

2012-05-30 15:34:09 UTC

Permalink

Dear Dave,

What is the name of the algorithm you have suggested in your comment below, where basically in order to find the magnitude and phase of a signal with a known frequency, you would perform a sample by sample multiplication of the signal and two sine waves (a sine and a cosine). I am interested in pursuing this a bit further. In particular, I am interested in finding the error that results in performing such operations.

Best wishes

Post by Dave Robinson

Hi,

I have sine wave as follows,

t=0:0.01:1;

x=sin(2*pi*4*t);

if i want to know the magnitude and phase of this signal, what do i do ??

It is something like,

[magnitude, phase] = function(x);

Can anyone tell me that, is there any such library function available in matlab which gives me magnitude and phase information of signal given.

Thanks !

Ashwini

If you know what the frequency of your signal is, as you do in your example, then create a unity amplitude sine wave and a unity amplitude cosine wave of exactly the same frequency but of zero phase. Now take your signal and do a sample by sample multiplication first by the cosine wave you have just synthesized, then accumulate the results - repeat this process for your sine wave. You should get 2 simple numbers out of each multiply accumulate operation. If you do a pythagorean sum on these numbers, it will tell you the amplitude of your signal, and if you take ATAN2 on the Cosine accumulation and the Sine accumulation, this will give you a measure of the phase.

If you don't know the frequency of your signal, then you probably need to resort to using the FFT, if so come back.

Regards

Dave Robinson

Rune Allnor

2009-07-06 11:45:28 UTC

Permalink

Post by Ashwini Deshpande

Hi,

I have sine wave as follows,

t=0:0.01:1;

x=sin(2*pi*4*t);

if i want to know the magnitude and phase of this signal, what do i do ??

It is something like,

[magnitude, phase] = function(x);

Can anyone tell me that, is there any such library function available in matlab which gives me magnitude and phase information of signal given.

There is no such function.

Some people have suggested FFT, but it only works like

you want it to, under very special cisrc*mstances.

For instance:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

N = 8;

f1 = 2/16;

x = sin(2*pi*f1*(0:N-1));

X = fft(x);

plot(abs(X)/N)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

In this case you recognize the amplitude from

the model, as expressed by Euler's relation

sin(x) = 1/2(exp(jx)-exp(-jx)).

However, if you change the frequency slightly,

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

N = 8;

f2 = 3/16;

y = sin(2*pi*f2*(0:N-1));

Y = fft(y);

plot(abs(Y)/N)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

you no longer recognize the single sinusodial

in your signal.

Rune

Greg

2009-07-07 04:52:59 UTC

Permalink

Post by Ashwini Deshpande

Hi,

I have sine wave as follows,

t=0:0.01:1;

x=sin(2*pi*4*t);

if i want to know the magnitude and phase of this signal, what do i do ??

It is something like,

[magnitude, phase] = function(x);

Can anyone tell me that, is there any such library function available in matlab which gives me magnitude and phase information of signal given.

Since you know the frequency, find C from

% x = C(1)*cos(2*pi*4*t)+C(2)*sin(2*pi*4*t);

t = (0:0.01:1)';

x = sin(2*pi*4*t);

W = [cos(2*pi*4*t) sin(2*pi*4*t)];

C = W\x

C =

0.0000

1.0000

Hope this helps.

Greg

Ashwini Deshpande

2009-07-20 08:35:19 UTC

Permalink

Post by Greg

Post by Ashwini Deshpande

Hi,

I have sine wave as follows,

t=0:0.01:1;

x=sin(2*pi*4*t);

if i want to know the magnitude and phase of this signal, what do i do ??

It is something like,

[magnitude, phase] = function(x);

Can anyone tell me that, is there any such library function available in matlab which gives me magnitude and phase information of signal given.

Since you know the frequency, find C from

% x = C(1)*cos(2*pi*4*t)+C(2)*sin(2*pi*4*t);

t = (0:0.01:1)';

x = sin(2*pi*4*t);

W = [cos(2*pi*4*t) sin(2*pi*4*t)];

C = W\x

C =

0.0000

1.0000

Hope this helps.

Greg

Thanks a lot Greg ,,

I got my result...

Ashwini !

11 Replies

3 Views

Permalink to this page

Disable enhanced parsing

Thread Navigation

Ashwini Deshpande2009-07-06 10:12:01 UTC