Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

plotting discrete time signals

n1 = -5:8;

x1 = zeros(size(n1));
x1(n1 >= -5 & n1 < 0) = -3 * n1(n1 >= -5 & n1 < 0);
x1(n1 >= 1 & n1 <= 5) = 3 * n1(n1 >= 1 & n1 <= 5);
x1(n1 >= 6 & n1 <= 8) = 5;

x2 = sin(0.2 * pi * n2) - 0.5 * cos(0.4 * pi * n2);

x3 = [1, 2, 0, -1, -2];

figure;

% Plot the first signal


subplot(2, 2, 1);
stem(n1, x1, 'r');
title('sequence 1');
xlabel('n');
ylabel('x1(n)');

prompt from user


K = input(prompt);

Discrete FFT
t = 0:1/512:(1-1/512);
x = 4*cos(2*20*pi*t - pi/8) + 6*cos(2*40*pi*t + pi/6) + 10*cos(2*120*pi*t + pi/4);

N=512;
y = fft(x,N)

freq = 0:(N/2-1)

stem(freq,(2/512)*abs(y(1:256))); % for plotting

when tolerance is given


tolerance = 0.00001
x1 = ceil(abs(y)-tolerance)
x2 = round(x1./(x1+1))
z = angle(y).*x2
pow = y.*conj(y)/(512*512)

plotting pow graph


stem(freq,2*pow(1:256));
Function definition to be done at the end
Function definitions (that returns a value)

function[xofn,index]= impulsesignal(sindex,lindex,rindex)
index=[lindex:rindex];
xofn =[(index-sindex)==0];
end

Function definitions (that does not return a value)

function plotCosineSequence(K, omega, theta)


n = 0:50;
x_n = K.^n .* cos(omega * n + theta);

if K > 1
titleText = 'Exponentially Increasing Sinusoidal Sequence';
elseif K < 1
titleText = 'Exponentially Decreasing Sinusoidal Sequence';
else
titleText = 'Sinusoidal Function';
end

subplot(1,2,1);
stem(n, x_n);
title(titleText);
xlabel('n');
ylabel('x2(n)');
grid on;
end

Frequency response of moving average filter

x = sin(2*pi*fm*t);

numM = ones(1,M);
denM = [M,zeros(1,M-1)];
[H,w] = freqz(numM, denM, N);
freq = (w/pi)*N/2;
m = abs(H);
subplot(3,1,2);
plot(freq,m);
xlim([0 fs/2]);

% Implement moving average filter


b = ones(1,M)/M;
y = filter(b, 1, x);
Y = fft(y,N);
FIR filter definition

b = fir1(44,[0.3 0.6],hann(45))
n = 0:44
stem(n,b);

% Implement FIR Filter


y1 = filter(b,1,x_noisy);

% fft of filtered signal


yft = fft(y1,N)
stem(freq,(2/fs)*abs(yft(1:fs/2)));

Frequency response of FIR filter

L = length(t);
n1 = 2^nextpow2(L)
[h,f] = freqz(b,1,n1,fs);
plot(f,20*log10(abs(h)));

convert string to double

str2double()
str2num()

You might also like