Effacer les filtres
Effacer les filtres

why do we get different values by angle command

2 vues (au cours des 30 derniers jours)
Kavita Guddad
Kavita Guddad le 7 Juin 2023
Commenté : Kavita Guddad le 9 Juin 2023
When i run this code i am getting the result as shown below
clc;clear all; close all;
N=input('Enter the fundamental period of the signal (N)');
M=input('Enter how many points of Fourier series to compute(m>N and m=multiples of N) ')
n=0:N-1;
x=[sin(2*pi*n/6)];
for k=0:M-1;
Sum=0;
for n=0:N-1;
Ck(k+1)=x(n+1)*exp(-j*2*pi*k*n/N);
Sum=Sum+Ck(k+1);
end
Ck(k+1)=Sum/N;
end
Mag=abs(Ck);Phase_ang=angle(Ck);
disp(Ck);disp(Mag);disp(Phase_ang);
Output
Enter the fundamental period of the signal (N)6
Enter how many points of Fourier series to compute(M=N or M=multiples of N) 6
M =
6
0.0000 + 0.0000i 0.0000 - 0.5000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.5000i
0.0000 0.5000 0.0000 0.0000 0.0000 0.5000
0 -1.5708 2.9229 1.5075 0.0588 1.5708
The values of phase angle are supposed to be
0 -1.5708 0 0 0 1.5708
But for the same array if i use command window i get the proper result
>>angle([0.0000 + 0.0000i 0.0000 - 0.5000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.5000i])
ans =
0 -1.5708 0 0 0 1.5708
  2 commentaires
Stephen23
Stephen23 le 7 Juin 2023
Modifié(e) : Stephen23 le 7 Juin 2023
"But for the same array..."
No, that is a different array, with different values. The values displayed (by default with four decimal places) are not the same as those stored in memory. So if you simply copy the displayed data, then you will have different values.
N=6;
M=6;
n=0:N-1;
x=sin(2*pi*n/6); % got rid of the superfluous square brackets
for k=0:M-1;
Sum=0;
for n=0:N-1;
Ck(k+1)=x(n+1)*exp(-j*2*pi*k*n/N);
Sum=Sum+Ck(k+1);
end
Ck(k+1)=Sum/N;
end
format long G
Ck
Ck =
Columns 1 through 3 5.55111512312578e-17 + 0i 5.55111512312578e-17 - 0.5i -1.66533453693773e-16 + 3.70074341541719e-17i Columns 4 through 6 1.85037170770859e-17 + 2.91747532808639e-16i 3.14563190310461e-16 + 1.85037170770859e-17i 2.22044604925031e-16 + 0.5i
Kavita Guddad
Kavita Guddad le 9 Juin 2023
Thank you very much stephen for the answer.Got my problem solved.

Connectez-vous pour commenter.

Réponse acceptée

Sandeep Mishra
Sandeep Mishra le 8 Juin 2023
Hello Kavita,
I understand that you are trying to create a Fourier series with M points, fundamental frequency N and you are also trying to find the phase angle and magnitude of the series.
In MATLAB, "angle" provides you with the functionality to find the phase angle of the complex array in [-π,π] interval.
In MATLAB, the default data type of numerical value is "double-precision floating-point" which requires 64 bits, So the value stored in your Sum variable and Ck series has more than 4 decimal points and it is different from your command line's input array.
You can round off the Ck array to 4 decimal places to get your desired results and compare the generated series by their difference as below.
clc;clear all; close all;
N= 6;
M= 6;
n=0:N-1;
x=[sin(2*pi*n/6)];
for k=0:M-1
Sum=0;
for n=0:N-1
Sum=Sum+x(n+1)*exp(-1j*2*pi*k*n/N);
end
Ck(k+1)=Sum/N;
end
Mag=abs(Ck);Phase_ang=angle(Ck);
% Input array
inputArray = [0.0000 + 0.0000i 0.0000 - 0.5000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.5000i];
% Difference between input array and generated result
difference = Ck - inputArray;
disp(difference);
1.0e-15 * 0.0555 + 0.0000i 0.0555 + 0.0555i -0.1665 + 0.0370i 0.0185 + 0.2917i 0.3146 + 0.0185i 0.2220 + 0.0000i
% Rounding off the value
Ck = round(Ck, 4);
disp(Ck);
0.0000 + 0.0000i 0.0000 - 0.5000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.5000i
% Angle calculated from input array
angleInput = angle(inputArray);
disp(angleInput)
0 -1.5708 0 0 0 1.5708
% Angle calculated from generated series
angleGiven = angle(Ck);
disp(angleGiven)
0 -1.5708 0 0 0 1.5708
You can refer to the documentation below to learn more about "angle", "double-precision floating-point", and "data-types" in MATLAB.
  1 commentaire
Kavita Guddad
Kavita Guddad le 9 Juin 2023
Thank you Sandeep for the answer. Got my problem solved.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by