graph shifting and for loops problem

조회 수: 1 (최근 30일)
Charles Moody
Charles Moody 2018년 2월 25일
편집: Walter Roberson 2018년 3월 22일
I have 2 questions:
1) In this code I use a for loop to populate a linespace that U must graph. But because logicals must be non zero, non negative I always have to start at element 1. But I have to plot it when element 1 in the linespace has to be at the o position on the graph.
2) I have nested loops that indicate the phase angle of a function. This is based off of the sign of the real and imaginary parts of the function. For some reason the phase as shown on the graph is wrong.
%constants
A =1;
B = .5;
j = sqrt(-1);
f1 = 1000; %Hz
f2 = 3000; %Hz
fs = 8000; %Hz
phi = .75*pi;
N1 = 8;
%time linespace
t = 0:1/fs:((N1-1)/fs);
g1 = sin(2*f1*pi*t)+B*sin(2*f2*pi*t + phi);
%for an 8-point DFT we need 12 input samples
xn1 = zeros(1,8);
for i = 1:8
xn1(i) = g1(i);
end
MN1_Matrix = zeros(8,8);
for i = 1:8
for k = 1:8
MN1_Matrix(i,k) = xn1(k)*(cos((((k-1)*2*pi*(i-1))/N1))-(j*sin(((k-1)*2*pi*(i-1))/N1)));
end
end
disp(MN1_Matrix)
for i = 1:8
X1_0(i) = cumsum(MN1_Matrix(1,i));
X1_1(i) = cumsum(MN1_Matrix(2,i));
X1_2(i) = cumsum(MN1_Matrix(3,i));
X1_3(i) = cumsum(MN1_Matrix(4,i));
X1_4(i) = cumsum(MN1_Matrix(5,i));
X1_5(i) = cumsum(MN1_Matrix(6,i));
X1_6(i) = cumsum(MN1_Matrix(7,i));
X1_7(i) = cumsum(MN1_Matrix(8,i));
end
X1_R = [sum(real(X1_0)) sum(real(X1_1)) sum(real(X1_2)) sum(real(X1_3)) sum(real(X1_4)) sum(real(X1_5)) sum(real(X1_6)) sum(real(X1_7))];
X1_I = [sum(imag(X1_0)) sum(imag(X1_1)) sum(imag(X1_2)) sum(imag(X1_3)) sum(imag(X1_4)) sum(imag(X1_5)) sum(imag(X1_6)) sum(imag(X1_7))];
for i = 1:8
X1_MAG(i) = sqrt((X1_R(i)^2) + (X1_I(i)^2));
end
for i = 1:8
if X1_I(i) == 0
X1_ANGLE = 0;
elseif X1_R(i) == 0 && X1_I(i) > 0
X1_ANGLE = -90;
elseif X1_R(i) == 0 && X1_I(i) < 0
X1_ANGLE = 90;
else
X1_ANGLE(i) = atand(X1_I(i)/X1_R(i));
end
end
subplot(2,2,1)
stem(X1_MAG)
xlabel('bin (m*1 kHz)')
ylabel('magnitude')
title('Magnitude of X(m)')
axis([0 9 0 4])
subplot(2,2,2)
stem(X1_ANGLE)
xlabel('bin (m*1 kHz)')
ylabel('phase')
title('Degree Phase of X(m)')
axis([0 9 -100 100])
subplot(2,2,3)
stem(X1_R)
xlabel('bin (m*1 kHz)')
ylabel('magnitude')
title('Real Part of X(m)')
axis([0 9 -.5 1.5])
subplot(2,2,4)
stem(X1_I)
xlabel('bin (m*1 kHz)')
ylabel('magnitude')
title('Imag Part of X(m)')
axis([0 9 -4 4])

채택된 답변

Walter Roberson
Walter Roberson 2018년 2월 25일
https://www.mathworks.com/help/matlab/ref/stem.html
You can pass x values to stem() to get it to plot with the coordinates you want.
If you use atan2d then you do not need all those if statements to get the proper angle.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by