0% found this document useful (0 votes)
110 views7 pages

Lab 1: Introduction To Matlab Engr 451: Zachary Armendariz and Luke Jocson

This document describes a Matlab lab assignment on signal processing of sequences. It introduces the sequence data structure and defines functions for basic operations like addition, subtraction, multiplication, and shifting sequences. These functions are tested on example sequences and the results are displayed.

Uploaded by

kevin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views7 pages

Lab 1: Introduction To Matlab Engr 451: Zachary Armendariz and Luke Jocson

This document describes a Matlab lab assignment on signal processing of sequences. It introduces the sequence data structure and defines functions for basic operations like addition, subtraction, multiplication, and shifting sequences. These functions are tested on example sequences and the results are displayed.

Uploaded by

kevin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab 1: Introduction to Matlab

Engr 451

Zachary Armendariz
and
Luke Jocson

Date Report Submitted: Feb. 10, 2017


ENGR 451 - Lab 1
Matlab tutorial

clear
x = sequence([1 2 3 4 5], 1);
y = sequence([5 3 1 -1 3 -2 2 3], -1);

% test plus
test_lab1('plus(x, y)')
test_lab1('plus(y, x)')
test_lab1('plus(1, x)')
test_lab1('plus(x, 1)')

y = sequence([5 3 1 0 3 -2 2 3], -4);


test_lab1('plus(x, y)')
test_lab1('plus(y, x)')

% test minustract
test_lab1('minus(x, y)')
test_lab1('minus(y, x)')
test_lab1('minus(1, x)')
test_lab1('minus(x, 1)')

% test timesiplication
test_lab1('times(x, y)')
test_lab1('times(3, x)')
test_lab1('times(x, 3)')

% test flip
test_lab1('flip(x)')

% test shift
test_lab1('shift(y, 2)')

%combinations
test_lab1('flip(minus(shift(plus(x, 2), 4), y))')
test_lab1('plus(flip(plus(x, y)), shift(y, -5))')
test_lab1('minus(plus(times(shift(flip(x), 4), shift(y, 3)), flip(y)), x)')

% test stem
set(clf, 'Position', [200 200 400 200])
stem(flip(2+(x-shift(y, -4).*y-3)))
title('y[n]');

% Program Listings
fprintf('\n\n')
disp('--- sequence.m --------------------------------')
type sequence

plus(x, y): sequence O.K.


plus(y, x): sequence O.K.
plus(1, x): sequence O.K.
plus(x, 1): sequence O.K.
plus(x, y): sequence O.K.
plus(y, x): sequence O.K.
minus(x, y): sequence O.K.
minus(y, x): sequence O.K.
minus(1, x): sequence O.K.
minus(x, 1): sequence O.K.
times(x, y): sequence O.K.
times(3, x): sequence O.K.
times(x, 3): sequence O.K.
flip(x): sequence O.K.
shift(y, 2): sequence O.K.
flip(minus(shift(plus(x, 2), 4), y)): sequence O.K.
plus(flip(plus(x, y)), shift(y, -5)): sequence O.K.
minus(plus(times(shift(flip(x), 4), shift(y, 3)), flip(y)), x): sequence O.K.

--- sequence.m --------------------------------

%Lab 1
%Zachary Armendariz
%Luke Jocson
%The purpose of this lab is to create the code to perform different
%functions on sequences with Matlab.
classdef sequence
properties %This first section of code was given
data %and identifies a sequence as a new
offset %Matlab structure.
end

methods
function s = sequence(data, offset)
s.data = data;
s.offset = offset;
end

function display(s) %The display portion of the code is


var = inputname(1); %only there to make the output of
if (isempty(var)) %sequence data more compact and
disp('ans ='); %readable.
else
disp([var '=']);
end
switch length(s.data)
case 0
disp(' data: []')
case 1
disp([' data: ', num2str(s.data)])
otherwise
disp([' data: [' num2str(s.data) ']'])
end
disp([' offset: ' num2str(s.offset)])
end

function y = flip(x)
% FLIP Flip a Matlab sequence structure, x, so y = x[-n]
z = x.data; %Declare a new array.
z(1:length(x.data)) = x.data(end : -1 : 1);
%This code stores the sequence into the array
%in the opposite order.
y = sequence(z, -(x.offset + length(x.data) - 1));
%The sequence is reconstructed with a new
end %offset based on the length and offset of
%the original sequence.
function y = shift(x, n0)
% SHIFT Shift a Matlab sequence structure, x, by integer amount n0 so that
y[n] = x[n - n0]
nf = x.offset + n0; %This finction simply adds a shift value
y = sequence(x.data, nf); %to the offset and reconstructs the
end %sequence.

function y = trim(x) %This function was designed to


indices = find(x.data); %remove zeros from the upper and
x.data(1:indices(1)-1) = []; %lower bounds of a sequence.
x.data(indices(end) - indices(1) + 2 : end) = [];
offset = x.offset + indices(1) - 1;
y = sequence(x.data, offset);
end

function z = plus(x, y)
% PLUS Add x and y. Either x and y will both be sequence structures, or
one of them may be a number.
if(isa(x, 'sequence') & isa(y, 'sequence'))
offset = 0; %Before two arrays can be added
lpad = x.offset - y.offset; %they have to be padded with zeros
%so they share the same length
%and offset.
rpad = length(y.data) + y.offset - length(x.data) - x.offset;
seq1 = [zeros(1, lpad) x.data zeros(1, rpad)];
seq2 = [zeros(1, -lpad) y.data zeros(1, -rpad)];
data = seq1 + seq2; %the arrays can be added easily when
if(x.offset < y.offset)%they are the same size.
offset = x.offset;
else %Next the larger offset has to be
offset = y.offset; %found to be used in forming the
end %resultant sequence.
if (sum(data) == 0) == length(data)
z = sequence(0, 0); %Finally the sequence is trimmed
else %and reconstructed. The if statement
%is for the unlikely case that the
z = trim(sequence(data, offset));%resultant sequence is
end %null.
elseif(isa(x, 'float'))
data = y.data + x; %If one of the inputs is a scalar instead
%of a sequence a different code is
%used.
if (sum(data) == 0) == length(data)
z = sequence(0, 0); %This code is very simple since
else %the scalar can just be added to
z = trim(sequence(data, y.offset));%all the elements
end %of the data portion of the sequence.
else
data = x.data + y;
if (sum(data) == 0) == length(data)
z = sequence(0, 0);
else
z = trim(sequence(data, x.offset));
end
end

end

function z = minus(x, y)
% MINUS Subtract x and y. Either x and y will both be sequence structures,
or one of them may be a number.
% The minus function is the same as the plus function, but the
% second element in the array needs to be inverted.
if(isa(x, 'sequence') & isa(y, 'sequence'))
yneg = y.data .* -1;
negseq = sequence(yneg, y.offset);
z = x + negseq;
elseif(isa(x, 'float'))
data = -1 .* y.data + x;
if (sum(data) == 0) == length(data)
z = sequence(0, 0);
else
z = trim(sequence(data, y.offset));
end
else
data = x.data - y;
if (sum(data) == 0) == length(data)
z = sequence(0, 0);
else
z = trim(sequence(data, x.offset));
end
end
end

function z = times(x, y)
% TIMES Multiply x and y (i.e. .*) Either x and y will both be sequence
structures, or one of them may be a number.
% Multiplying is again very similar to the plus function, but
% using the .* operator.
if(isa(x, 'sequence') & isa(y, 'sequence'))
offset = 0;
lpad = x.offset - y.offset;
rpad = length(y.data) + y.offset - length(x.data) - x.offset;
seq1 = [zeros(1, lpad) x.data zeros(1, rpad)];
seq2 = [zeros(1, -lpad) y.data zeros(1, -rpad)];
data = seq1 .* seq2;
if(x.offset < y.offset)
offset = x.offset;
else
offset = y.offset;
end
if (sum(data) == 0)
z = sequence(0, 0);
else
z = trim(sequence(data, offset));
end
elseif(isa(x, 'float'))
data = y.data .* x;
if (sum(data) == 0) == length(data)
z = sequence(0, 0);
else
z = trim(sequence(data, y.offset));
end
else
data = x.data .* y;
if (sum(data) == 0) == length(data)
z = sequence(0, 0);
else
z = trim(sequence(data, x.offset));
end
end
end

function stem(x)
% STEM Display a Matlab sequence, x, using a stem plot.
% Finally it is important to create a function for plotting the
% results. This function simply creates the appropriate
% abscissa using the ofset of the sequence.
m = x.offset : x.offset + length(x.data) - 1 ;
stem(m, x.data, 'filled');
axis tight
xlim([(m(1) - 1) (m(end)+1)]);
ylim([(min(x.data) - 1) (max(x.data) + 1)]);
end
end
end
Published with MATLAB® R2016a

You might also like