How to plot this signal in Matlab?

Asked

Viewed 272 times

0

I wanted to plot these signals in Matlab to make a series of Urier, but I’m not getting it. Does anyone know what the mistake is? Also, how do I include the imaginary unit? I put as 1j but I don’t know if it’s right

clear all;
close all;

T=1;
w0=2*pi/T;
c=4;
T1=T/c;
t=linspace(0,2*T,1000);

N=5;
a=zeros(N);

a(0)=2/c;

for i=1:N-1
    a(i)=(2/c)*sin(w0*i*T1)/(w0*i*T1);
end

a_neg=zeros(N);
a_neg(0)=0; 

for i=1:N-1
    a_neg(i)=(2/c)*sin(-w0*(i)*T1)/(-w0*(i)*T1);
end

x_aprox_0=a(0)+a_neg(0);
x_aprox_1=a(0)+a_neg(0)+a(1)*exp(1j*w0*t)+a_neg(1)*exp(-1j*w0*t);
x_aprox_2=a(0)+a_neg(0)+a(1)*exp(1j*w0*t)+a_neg(1)*exp(-1j*w0*t)+a(2)*exp(2*1j*w0*t)+a_neg(2)*exp(-2*1j*w0*t);
x_aprox_3=a(0)+a_neg(0)+a(1)*exp(1j*w0*t)+a_neg(1)*exp(-1j*w0*t)+a(2)*exp(2*1j*w0*t)+a_neg(2)*exp(-2*1j*w0*t)+a(3)*exp(3*1j*w0*t)+a_neg(3)*exp(-3*1j*w0*t);
x_aprox_4=a(0)+a_neg(0)+a(1)*exp(1j*w0*t)+a_neg(1)*exp(-1j*w0*t)+a(2)*exp(2*1j*w0*t)+a_neg(2)*exp(-2*1j*w0*t)+a(3)*exp(3*1j*w0*t)+a_neg(3)*exp(-3*1j*w0*t)+a(4)*exp(4*1j*w0*t)+a_neg(4)*exp(-4*1j*w0*t);

figure;
plot(t,x_aprox_0);
plot(t,x_aprox_1);
plot(t,x_aprox_2);
plot(t,x_aprox_3);
plot(t,x_aprox_4);
hold on;
grid on;

1 answer

0


Your script has several errors, but they are all easily solved.

I will initially put some tips on how to solve problems when programming!


  • Pay attention to the return of Matlab

When you spin something and fail, the interpreter returns an error.

In your case:

Subscript indices must either be real Positive integers or logicals.

Other than starting at 0, starts counting in 1!

From now on, I assume all the errors related to this have been corrected!


  • Check how a function works before using

You can use help nome_da_funcão on the command line to see what she needs input and what she returns as output.

You use

a=zeros(N); %isso é uma matrix 5x5

However, you want to use

a=zeros(N,1); % ou  a=zeros(1,N); 

That gives you a column (or row). This is what you are using as coefficients.

  • Beware of messing with imaginary things

    Plot(column,imag_number_column)

will have the result

 plot(column,real(imag_number_column))

Little things it’s good to know:

clear all

In general, use only clear, because Matlab keeps part of the code in memory to optimize the time to interpret & run.

figure, plot and hold.

When using several graphs in the same figure, use this in the sequences:

figure
plot 
hold
plot

or

figure
hold
plot
plot

or even without declaring figure. Matlab will create a picture if there is none when hold or Plot is called. And the chart execution is immediate (different from, for example, who needs draw to draw the graph). When a Plot is not in hold, it will erase what is already plotted.

plot(t,x_aprox_0);

Look what you want to plot is the same thing you stated. In this case, x_aprox_0 is just a point. I don’t think it’s the result you want, but it’s what you stated in

x_aprox_0=a(1)+a_neg(1) ;

where the two values are constant (see that already corrected the index).

x_aprox_1=a(1)+a_neg(1)+a(2)*exp(1j*w0*t)+a_neg(2)*exp(-1j*w0*t);

Note that the values make sense. In this case, a_neg(2) and a(2) are close to zero.


Let’s go to the rest of your code. You haven’t made it clear what the code does, and you can edit your question and comment on my answer to understand us better.

I understand you have to have the signal on different frequencies to make a Fourier transform. You’re failing at this, but since I’m not sure that’s what you want, I’m going to write my letters. If that’s the case, I’ll put the code in later, editing my answer:

  1. You want 4 frequencies plus the original sine.

  2. Each signal will be added to the previous signal.

  3. Plot will contain the 5 signals, the original plus the sums.

If this is the case, there are some ways to write the answer. Yours is not far from it, but the frequencies are not right. But if you want to modulate the intensity, your result is closer. But you have values very close to zero.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.