Matlab examples
Matlab examples

Example 1

Develop a code in MATLAB for one dimensional, discrete unit step function.

Result: Discrete unit step function

solution

SOLUTION
t=-10:20;     %definition of time period 
step=heaviside(t);    %heaviside(x) is Matlab function 

and has the value 0 for x < 0, 1 for x > 0,
and 0.5 for x = 0. step(t==0)=1; figure; %command for drawn picture stem(t,step); grid on; xlabel('n'); ylabel('f(n)'); title('1D discrete step function'); axis([-10 10 -1 3]);

Example 2

Develop a code in MATLAB for sine wave generation.

Result: Two harmonic frequencies forming signal

solution

SOLUTION
range=6*pi;             %max. time of the signal
t=0:0.001:range;        %time points
A=[1 0.13];             %vector of amplitudes
w=[1 3];                %vector of frequencies [Hz]
phi=[0 0];              %vector of phases
sig1=A(1)*sin(w(1)*t+phi(1));  % definition of particular signals
sig2=A(2)*sin(w(2)*t+phi(2));
signal=sig1+sig2;
figure;
plot(t,sig1,':r','LineWidth',2);
hold on;
plot(t,sig2,'--g','LineWidth',2);
hold on;
plot(t,signal,'LineWidth',2);
grid on;
axis([0 rozsah -1.2*sum(A) 1.2*sum(A)]);  
xlabel('t [s]');
ylabel('f(t)');
title('Harmonic signals and their sum');

Example 3

Develop a code in MATLAB for Discrete Fourier transformation and frequency characteristics.

Examples of transfer function of LDTI, magnitude and phase frequency characteristic

solution

SOLUTION
count=32;
Ts=4/count;   %sampling frequency
per=4;        %number of periods
syms k;       %symbol variable
syms n;
signal=[3.*ones(1,count/4) 2.*ones(1, count /4) ones(1, count /4) zeros(1, count /4)];
fn=[];
for n=1:per
    fn=[fn signal];
end
n=0:count*per-1;

figure;
stem(n,fn);
title('Discrete signal');
axis([0 length(fn) min(abs(fn))-0.5 max(abs(fn))+0.5]);
grid on;

figure;
Xk=fft(signal);     %discrete Fourier transformation 
os=0:length(Xk)-1;
stem(os,abs(Xk));   %magnitude frequency characteristic
title('Magnitude frequency characteristic');
grid on;

figure;
stem(os,angle(Xk));   %phase frequency characteristic
title('Phase frequency characteristic');
grid on;