why do we get different values by angle command
1 view (last 30 days)
Show older comments
Kavita Guddad
on 7 Jun 2023
Commented: Kavita Guddad
on 9 Jun 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 Comments
Stephen23
on 7 Jun 2023
Edited: Stephen23
on 7 Jun 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
Accepted Answer
Sandeep Mishra
on 8 Jun 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);
% Rounding off the value
Ck = round(Ck, 4);
disp(Ck);
% Angle calculated from input array
angleInput = angle(inputArray);
disp(angleInput)
% Angle calculated from generated series
angleGiven = angle(Ck);
disp(angleGiven)
You can refer to the documentation below to learn more about "angle", "double-precision floating-point", and "data-types" in MATLAB.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!