You are on page 1of 11

Convolution of signals Hw2prob1

n = 0 :0.1:10; %n is the sample number


hn = (-1/2).^n;

figure
stem(n,hn)
xlabel('n');
ylabel('Amplitude');
title('Signal hn')

xn = (1/2).^n;

figure
stem(n,xn)
xlabel('n');
ylabel('Amplitude');
title('Signal xn')

yn=conv(xn,hn)
figure
stem(yn)
xlabel('n');
ylabel('Amplitude');
title('Signal yn')
Plotting functions

Signal 1
syms t
x=exp(-2*t);
x=(x).*(heaviside(t))
ezplot(x,[-1 5 0 1.5])
figure
h=exp(-t).*cos(t).*t;
h=(h).*(heaviside(5*pi*t));
ezplot(h,[-1 5 0 1.5])
Signal 2

T = 0.001; % sampling period in seconds


n = 0 : 2000; %n is the sample number
x1 = exp(-pi*n*T) + 5*sqrt(cos(n*T));
t = n*T;
figure
plot(t,x1)
xlabel('Time (seconds)');
ylabel('Amplitude');
title('Signal x1')

T = 0.001; % sampling period in seconds


n = 0 : 2000; %n is the sample number
%omega = 3;
omega = 0 + (100-0).*rand(1,1);
x2 = cos(omega*n*T + 0.4);
t = n*T;
figure
plot(t,x2)
xlabel('Time (seconds)');
ylabel('Amplitude');
title('Signal x2')

figure
plot(t,x2+x1)
title('x1+x2')
figure
plot(t,x2.*x1)
title(' x1*x2')
Square signals
repeating_segment = [ zeros(1, 100) ones(1, 100)];
num_periods = 10;
square_waveform = []; % empty variable
for k = 1 : num_periods
square_waveform = [square_waveform repeating_segment];
end
plot(square_waveform);
ylim([-0.1 1.1])

Triangular Signal

repeating_segment = [ 0:5 4:-1:1]; % same as [ 0 1 2 3 4 5 4 3 2 1 ]


num_periods = 10;
trangular_waveform = []; % empty variable
for k = 1 : num_periods
trangular_waveform = [trangular_waveform repeating_segment];
end
plot(trangular_waveform);
Impulse response function

function [x,n] = impseq(n0,n1,n2)

n = [n1:n2];
x = [(n-n0) == 0];
end

HW2 prob5 (sketching graphs)

figure(1)
y=impseq(0,-5,5)+impseq(3,-5,5)
stem([-5:5],y)

figure(2)
syms t
k=heaviside(t+1)-heaviside(t-3);
ezplot(k,[-5 5 0 2])

figure(3)
w=impseq(0,-5,5)+(1/2).*impseq(1,-5,5)+(1/2^2).*impseq(2,-
5,5)+(1/2^3).*impseq(3,-5,5)+(1/2^4).*impseq(4,-5,5)
stem([-5:5],w)

figure(4)
syms t
x=(exp(-t)).*(heaviside(t))
ezplot(x,[-1 5 0 1.5])
Frequency Analysis

t=-1:0.001:1;
x=sin(2*pi*50*t);
%x=sin(2*pi*50*t)+sin(2*pi*75*t);
%x=sin(2*pi*20*t)+4*cos(2*pi*50*t)+2*sin(2*pi*100*t)+7*randn(size(t));
figure
plot(t(1001:1200),x(1:200))
grid
title('Sin(2\pi50t)')
xlabel('Time, s')
figure
X=abs(fft(x));

X2=fftshift(X);
f=-499.9:1000/2001:500;
plot(f,X2);
grid
title(' Frequency domain representation of Sin(2\pi50t)')
xlabel('Frequency, Hz.')
Bass Guitar example
%http://eleceng.dit.ie/dorran/matlab/bass.wav

fs = 44100; % the signal is known to have been recorded at this rate


[bass_guitar] = audioread('bass.wav'); %read in first 5 seconds; fs =
44100Hz
ft = fft(bass_guitar);
mag_ft = abs(ft);

N = length(mag_ft);
freq_scale = 0: fs/(N-1):fs;
figure(3)
plot(freq_scale, mag_ft);
ylabel('Magnitude/Amplitude')
xlabel('Frequency (Hz)')

figure(5)
plot(bass_guitar);
ylabel('Amplitude')
xlabel('Sample Number')

first_note = bass_guitar(1:38000); %exrtact the first note from the signal


%first_note = bass_guitar(2100000:2260000);
ft_first_note = fft(first_note);
mags = abs(ft_first_note);
freq_scale = 0: fs/(length(mags) -1) : fs;

figure(6)
plot(freq_scale, mags)
ylabel('Magnitude/Amplitude')
xlabel('Frequency (Hz)')

You might also like