How to plot this signal in MatLab?

0

I wanted to plot these signals in MatLab to do a series of fourier, but I can not. Does anyone know the error? Also, how do I include the imaginary unit? I put like 1j but I do not 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;
    
asked by anonymous 26.04.2018 / 17:28

1 answer

0

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

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

  • Pay attention to Matlab's return

When you run something and crash, interpreter returns an error.

In your case:

  

Subscript indices must either be real positive integers or logicals.

Unlike starting at 0,

From now on, I assume all bugs related to this have been fixed!

  • Check how a function works before using

You can use help nome_da_funcão on the command line to see what it needs to input and what it 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 line). 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))

Smaller things that are good to know:

clear all

In general, use only clear , as Matlab saves part of the code in memory to optimize the time to interpret & roll.

  

figure , plot and hold .

When using multiple graphics in the same figure, use this in sequences:

figure
plot 
hold
plot

or

figure
hold
plot
plot

or even without declaring figure . Matlab will create a figure if there is none when hold or plot is called. And the execution of the chart is immediate (unlike, for example, that needs draw to draw the graph). When a plot is not in hold , it will delete 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 only one point. I do not think it's the result you want, but that's what you stated in

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

where the two values are constant (see that I have 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);

See 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 have not made clear what the code does, and you can edit your question and comment on my answer to understand us better.

So I understand you have the signal at different frequencies to make a Fourier transform. In that you are failing, but as I am not sure if this is what you want, I will write my ideas. If so, I'll post the code later, editing my answer:

  • You want 4 frequencies plus the original sine.

  • Each signal will be added to the previous signal.

  • The plot will contain the 5 signals, the original plus the sums.

  • If this is the case, there are a few ways to write the answer. Yours is not far from that, 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.

        
    27.04.2018 / 15:01