Code covered by the BSD License  

Highlights from
MatLab Solutions: "Introduction to Digital Signal Processing: A Computer Laboratory Textbook".

image thumbnail

MatLab Solutions: "Introduction to Digital Signal Processing: A Computer Laboratory Textbook".

by

 

29 Oct 2012 (Updated )

These files are the MatLab solutions of exercises contained in the above DSP lab textbook.

my_conv2(x,h,nx,nh)
function [ny y] = my_conv2(x,h,nx,nh)
% This function computes the linear convolution of input signals x[n] and h[n].
% Outputs:
% y: is the linear convolution between sequences x[n] and h[n] in a vetor format.
% ny: is a vector containing the time indices of the linear convolution of x[n] and h[n].
% Inputs:
% x: vector containing the input signal x[n].
% h: vector containing the input signal h[n].
% nx: vector conatining the time indices of the input signal x[n].
% nh: vector conatining the time indices of the input signal h[n].
x = x(:).';
h = h(:).';

N = length(x);
M = length(h); 
L = N+M-1;

% The time indices of the convolution sum are computed as:
ny = nx(1)+nh(1):nx(end)+nh(end);

if N>M 
    % make both sequences of length N+M-1 by zero padding as follows:
     long_ext = [zeros(1,M-1) x];
    short_ext = [zeros(1,N-1) h]; 
else
    % make both sequences of length N+M-1 by zero padding as follows:
     long_ext = [zeros(1,N-1) h];
    short_ext = [zeros(1,M-1) x];
end

y = zeros(1,L);
short_ext_rev = fliplr(short_ext); % This is h[-n].

for k=1:L
    y(k) = long_ext*shiftright(short_ext_rev,k-1).';
end

Contact us