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:
How to get magnitude and phase onfo of a given signal

Subject: How to get magnitude and phase onfo of a given signal

From: Ashwini Deshpande

Date: 6 Jul, 2009 10:12:01

Message: 1 of 12

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

Subject: How to get magnitude and phase onfo of a given signal

From: Wayne King

Date: 6 Jul, 2009 11:18:01

Message: 2 of 12

hi Ashwini, please look at the help for fft

>>doc fft

Hope that helps,
wayne



"Ashwini Deshpande" <vd.ashwini@mathworks.com> wrote in message <h2silh$5lf$1@fred.mathworks.com>...
> 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

Subject: How to get magnitude and phase onfo of a given signal

From: Dave Robinson

Date: 6 Jul, 2009 11:23:01

Message: 3 of 12

"Ashwini Deshpande" <vd.ashwini@mathworks.com> wrote in message <h2silh$5lf$1@fred.mathworks.com>...
> 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

Subject: How to get magnitude and phase onfo of a given signal

From: Rune Allnor

Date: 6 Jul, 2009 11:45:28

Message: 4 of 12

On 6 Jul, 12:12, "Ashwini Deshpande" <vd.ashw...@mathworks.com> wrote:
> 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 cisrcumstances.

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

Subject: How to get magnitude and phase onfo of a given signal

From: Ashwini Deshpande

Date: 6 Jul, 2009 12:21:01

Message: 5 of 12

"Dave Robinson" <dave.robinson@somewhere.biz> wrote in message <h2smql$ae4$1@fred.mathworks.com>...
> "Ashwini Deshpande" <vd.ashwini@mathworks.com> wrote in message <h2silh$5lf$1@fred.mathworks.com>...
> > 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

Subject: How to get magnitude and phase onfo of a given signal

From: Dave Robinson

Date: 6 Jul, 2009 16:29:03

Message: 6 of 12

"Ashwini Deshpande" <vd.ashwini@mathworks.com> wrote in message <h2sq7d$8v4$1@fred.mathworks.com>...
> "Dave Robinson" <dave.robinson@somewhere.biz> wrote in message <h2smql$ae4$1@fred.mathworks.com>...
> 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

Subject: How to get magnitude and phase onfo of a given signal

From: TideMan

Date: 6 Jul, 2009 20:19:55

Message: 7 of 12

On Jul 7, 4:29 am, "Dave Robinson" <dave.robin...@somewhere.biz>
wrote:
> "Ashwini Deshpande" <vd.ashw...@mathworks.com> wrote in message <h2sq7d$8v...@fred.mathworks.com>...
> > "Dave Robinson" <dave.robin...@somewhere.biz> wrote in message <h2smql$ae...@fred.mathworks.com>...
> > 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.

Subject: How to get magnitude and phase onfo of a given signal

From: Ashwini Deshpande

Date: 7 Jul, 2009 04:49:02

Message: 8 of 12


> >
> > 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.

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

Subject: How to get magnitude and phase onfo of a given signal

From: Greg

Date: 7 Jul, 2009 04:52:59

Message: 9 of 12

On Jul 6, 6:12 am, "Ashwini Deshpande" <vd.ashw...@mathworks.com>
wrote:
> 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

Subject: How to get magnitude and phase onfo of a given signal

From: Ashwini Deshpande

Date: 20 Jul, 2009 08:35:19

Message: 10 of 12

Greg <heath@alumni.brown.edu> wrote in message <906db421-8260-40f2-98ae-182ac2a2adb7@h8g2000yqm.googlegroups.com>...
> On Jul 6, 6:12?am, "Ashwini Deshpande" <vd.ashw...@mathworks.com>
> wrote:
> > 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 !

Subject: How to get magnitude and phase onfo of a given signal

From: Srikanth

Date: 24 Apr, 2012 18:41:07

Message: 11 of 12

TideMan <mulgor@gmail.com> wrote in message <a2c6b110-3823-4ae8-8ff7-9b9f416b39db@p36g2000prn.googlegroups.com>...
> On Jul 7, 4:29 am, "Dave Robinson" <dave.robin...@somewhere.biz>
> wrote:
> > "Ashwini Deshpande" <vd.ashw...@mathworks.com> wrote in message <h2sq7d$8v...@fred.mathworks.com>...
> > > "Dave Robinson" <dave.robin...@somewhere.biz> wrote in message <h2smql$ae...@fred.mathworks.com>...
> > > 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.

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

Subject: How to get magnitude and phase onfo of a given signal

From: Ihaveideas

Date: 30 May, 2012 15:34:09

Message: 12 of 12

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

"Dave Robinson" wrote in message <h2smql$ae4$1@fred.mathworks.com>...
> "Ashwini Deshpande" <vd.ashwini@mathworks.com> wrote in message <h2silh$5lf$1@fred.mathworks.com>...
> > 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

Tags for 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