Hello
I'm trying to plot these polynomials shown in the image, but I keep receiving errors and I'm not if my code is correct of not. Could you please help ?
regards
clear all
close all
clc
%%
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1024; % Length of signal
t = 2*(0:L-1)*T; % Time vector
x = 0;
c = 1+i;
P(1) = 1;
Q(1) = 1;
P(2) = P(1) + exp(i*(2^(0))*t)*Q(1);
Q(2) = P(1) - exp(i*(2^(0))*t)*Q(1);
P(3) = P(2) + exp(i*(2^(1))*t)*Q(2);
Q(3) = P(2) - exp(i*(2^(1))*t)*Q(2);
P(4) = P(3) + exp(i*(2^(2))*t)*Q(3);
Q(4) = P(3) - exp(i*(2^(2))*t)*Q(3);
for m=1:16
x = x +c*exp(i*2*pi*m*t).*P(m);
end
figure
subplot(2,2,1)
plot(t,x)
title('signal')

댓글 수: 6

Image Analyst
Image Analyst 2022년 1월 23일
편집: Image Analyst 2022년 1월 23일
What is this "x" that you're overwriting on each iteration of the for loop? I thought you wanted to compute p and q, not x.
And MATLAB thinks i is a variable. So if you want to use it as a variable you need to use
i = sqrt(-1);
or else use a 1 in front of it: 1i
Matt J
Matt J 2022년 1월 23일
They don't look like polynomials to me. Also, they are complex-valued, so what does it mean to "plot" them? If you want to plot their absolute value, do plot(t,abs(x)).
Mohamed Ahmed
Mohamed Ahmed 2022년 1월 23일
I did try plot them but I keep getting this error "Unable to perform assignment because the left and right sides have a different number of elements"
Mohamed Ahmed
Mohamed Ahmed 2022년 1월 23일
x is waveform I want to plot which will contait the frequency components.
I tried using i = sqrt(-1);
but it wasn't the problem, I keep getting this error "Unable to perform assignment because the left and right sides have a different number of elements" for this line ==> "P(2) = P(1) + exp(i*(2^(0))*t)*Q(1);"
Matt J
Matt J 2022년 1월 23일
That is correct. P(2) is a scalar location. It cannot hold a vector
Mohamed Ahmed
Mohamed Ahmed 2022년 1월 23일

댓글을 달려면 로그인하십시오.

 채택된 답변

Matt J
Matt J 2022년 1월 23일
편집: Matt J 2022년 1월 23일

0 개 추천

Change P(m) to P{m} and Q(m) to Q{m} everwhere.
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1024; % Length of signal
t = 2*(0:L-1)*T; % Time vector
x = 0;
c = 1+i;
P{1} = 1;
Q{1} = 1;
for m=2:16
P{m} = P{m-1} + exp(1i*(2^(m-2))*t).*Q{m-1};
Q{m} = P{m-1} - exp(1i*(2^(m-2))*t).*Q{m-1};
end
for m=1:16
x = x +c*exp(1i*2*pi*m*t).*P{m};
end
whos x
Name Size Bytes Class Attributes x 1x1024 16384 double complex

댓글 수: 4

Mohamed Ahmed
Mohamed Ahmed 2022년 1월 23일
I received this error "Index exceeds the number of array elements (2)"
Matt J
Matt J 2022년 1월 23일
Make sure you're using my latest iteration of the code.
Mohamed Ahmed
Mohamed Ahmed 2022년 1월 23일
편집: Mohamed Ahmed 2022년 1월 23일
Yes, It worked, I also plotted the magnitude and phase spectrum of the signal.
Is this plot considered a flat spectrum ?
Thank you :)
Matt J
Matt J 2022년 1월 23일
I'm glad it worked, but please Accept click the answet to indicat so.
Whether it's to be considered flat is probably subjective...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Annotations에 대해 자세히 알아보기

질문:

2022년 1월 23일

댓글:

2022년 1월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by