What does this code mean?

clc
t=linspace(-1,1,256);%%%
F=1; %%% kare dalga frekansı
A=2; %%% kare dalga genliği
harmonik=3;
kare=zeros(1,length(t));
for k=1:harmonik
coeff=(2*A)/(k*pi)*sin(k*pi/2);
kare=coeff*cos(k*2*pi*F*t)+kare;
end k_d=A/2+kare;
figure(1)
plot(t,kare);
grid on
close all
time=20;
Ts=1/100; %%% örnekleme frekansı
t=0:Ts:(time-Ts);
x=2*[t <= 2]; %%%% kare dalga genliği ve zaman aralığı
N=length(x);
ssf=((-N/2):(N/2-1))/(Ts*N);
fx=Ts*fft(x(1:N));
fxs=fftshift(fx);
figure
subplot(2,1,1);
set(plot(t,x),'LineWidth',1.5);
xlabel('sn');
subplot(2,1,2);
set(plot(ssf,abs(fxs)),'LineWidth',1.5);
xlabel('Frekans [Hz]'); ylabel('Genlik')
What does this code do? I need only variables like fx, ssf etc. What are these for?

 Risposta accettata

Image Analyst
Image Analyst il 18 Nov 2020

1 voto

The first part of the code makes an x axis, "t" and then does a loop where it computes a single value for kare, because it didn't index kare. If it had used kare(k) = ..... then kare would have been a vector and the plot would plot kare vs. t.
The second part computers fx and ssf according to the formulas and functions so it sounds like that's what you need. If you need more specifics, can you ask the author? Or just run it and see what it does?

4 Commenti

I need somethings like that. My teacher wants all meanings one by one. So right, i need more specifics. But thanks anyway.
Image Analyst
Image Analyst il 19 Nov 2020
If your teacher is trying to get you in the habit of commenting your code, then I would say that that is a very very good teacher. Putting in lots of comments (while you're typing, not hopefully after you're done with the whole program because that rarely ever happens) is one of my two main items of advice for programmers. The other is that you use descriptive variable names, not one or two letter, cryptic variable names which just makes an alphabet soup mess of a program. Thank your teacher also for teaching you a skill that hopefully you will retain and use for the rest of your life. Far too many programmers write unmaintainable code without sufficient comments and with cryptic variable names, making code that is impenetrable and difficult or impossible to understand and maintain.
Steven Lord
Steven Lord il 19 Nov 2020
Commenting your code as you're writing it is good. IMO commenting your code before you write it, so you keep track of what steps you need to implement, can be even better.
Thank you both. I'll keep working on it. I hope i can understand all the codes with comments because we don't take the lesson in english, so its kinda hard time to time.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by