Uploaded by carlcort99

Report ECE 2026 Lab 5

Lab #5
ECE-2026 Spring-2023
LAB COMPLETION REPORT
Name: Ivan Mix, Carl Cort, Christian Choi, Adnan Porbanderwala
Date of Lab: 16 March 2023
Part 5.1
Part 3.2 Process one row of the input image echart with a 1-D first-difference filter. Explain how the
output from the filter makes it easy to measure the width of black regions. Use MATLAB’s find function
to help in determining the width of the black “E” from the impulses in the first-difference output.
Echart Image (shown via imread)
The polarity will tell whether it is transitioning from white to black. If it is a transition from white to
black, it will negative because it will be a zero value minus a positive --> negative, and if it is transitioning
from black to white it will be positive because it will be a zero value minus a negative --> positive value. The
difference between the original sampled row and the filtered row in the images below make this easier to
see.
We can use this to calculate the width of "E" by finding the first negative value in the filtered row
(the first transition from white to black) and the first positive value not at t=0 (the first transition from white
to black) and find the difference in their sample number to find the width of the first character, which in all
the rows is 'E'.
To find the full width, make sure to choose a row that hits the top or bottom of the E because those
are the widest parts. The row values given coincided with the center of the ‘E’s (checked by visualizing the
row being sampled in another plot). I was not sure if that is what you wanted, so I did the calculations with
that and found widths of 18 for the top complete row, 14 for the middle and 10 for the bottom row. All in
units of samples. Using my own values that actually sample rows that are more representative of the width of
the 'E' by sampling rows that hit the top of the 'E's (these rows are listed in a comment above where the m
variable is initialized) the width of the first 'E' is around 33, the second is about 26, and the third is about 20
samples.
Images of the row being sampled. On the left is the row provided in the sample code (65) and
on the right is the row I chose (46). As you can see, the sample code’s row cuts through the
center of the ‘E’, not the top, missing the majority of its width. Even the top isn’t the
entirety of the width of the E, but it is much closer.
Code:
%% 3.2
clc;clear;close all
load echart.mat
bdiffh = [1, -1];
echartSize = size(echart)
%Size 257 by 256, so the
figure()
imshow(echart)
title("echart")
% 46 is a row that actually catches the top of the 'E', resulting in
% finding something much closer to the "width" of the 'E'. The following
% list of three values actually coincide with the tops of the 'E's.
% 46, 131, 209
m = 209; % 65, 147, 221
yy1 = conv( echart(m,:), bdiffh );
%% Plot the input and output in the same figure using subplot
figure()
title("Row Comparison")
subplot(1, 2, 1)
plot(echart(m,:))
title("Original Row")
subplot(1,2,2)
plot(yy1)
title("Edge-Detected Row")
%% Visualizing the row being sampled in context of the image
%I am plotting the echart again but with the sampled row indicated by a
%black line
echart(m, :) = zeros(1, echartSize(2));
figure()
imshow(echart)
title("Row-Indicated Echart")
%Based on this, the row width being found will be the width of the center
%of the E, not the width of the character as a whole. I am going to
%continue the lab as though that is what I am supposed to do... but still
%this doesn't seem like it's really finding the "width" of the E. Granted
%no single row could because the little spikes coming off the top and
%bottom of the 'E' are slanted, so even the top of the E doesn't indicate
%the true "width" of the whole character.
%% Explainations of the idea behind polarity of the filtered values and
%%finding the width of a character
%The polarity will tell whether it is transitioning from white to black
%because if it is a transition from white to black, it will negative
%because it will be a zero value minus a positive --> negative, and if
%it is transitioning from black to white it will be positive because it
%will be a zero value minus a negative --> positive value.
%% Find the width of "E"
%You could find the width of "E" by looking for the first negative value
in
% the filtered row (the first transition from white to black) and the
first
% positive value not at t=0 (the first transition from white to black) and
% find the difference in their sample number to find the width of the
first
% character, which in all the rows is 'E'. To find the full width, make
% sure to choose a row that hits the top or bottom of the E because those
% are the widest part.
%% Calculating the width of the first character in the sampled row - Code
%Returns the first three indices of non-zero values in the filtered row
%because I know the structure of the data, I know this will be the initial
%index where the filter is running onto the data, the first edge of the
%'E', and the second edge of the 'E'. Therefore the difference between the
%2nd and 3rd entry will be the width of the piece between the edges, which
%will be the width of the E if the correct row is sampled.
%The following code outputs width of 18 for the top complete row, 14 for
% the middle and 10 for the bottom row. All in units of samples.
%Using my own values that actually sample rows that are more
representative
%of the width of the 'E' by sampling rows that hit the top of the 'E's
%(these rows are listed in a comment above where the m variable is
%initialized) the width of the first 'E' is around 33, the second is about
%26, and the third is about 20 samples.
indices = find(yy1, 3, "first");
width = indices(3) - indices(2)
%help find
Part 3.3 (Optional) – didn’t do
Part 3.4 LTI Frequency Response Demo
Image of the demo GUI when the input signal is 1.8*cos(0.2*pi*n – 0.6*pi) as it is for parts c
through d.
Image of the null frequency for the 7-point averaging filter. This one specifically uses 2pi/7 freq.
Image of the frequency 0.4pi being nulled by a 5-point averaging filter
Written Answers to Parts/Code (answers in Matlab Code Comments):
%% 3.4
dltidemo
%% Answer questions and attach GUI screenshots for the lab report
%(b) input freq of 0.2pi
% Magnitude Response: 0.374
% Phase Response: -1.885 ~ -0.6pi
%% Explaination of the delay from the filter AND part (d)
%Given a 7 point averaging filter, you would factor out a e^j(-3*omega) to
% create conjugate symetry in the terms so that they can be converted to
% real-valued sinusoids to make the amplitude response a real-valued
% function. The remaining part (the factored out complex number) is what
% creates the delay. This delay will always be 3 samples since the phase
% equals omega * (-3) and time delay (in this case sample delay since it’s
% a discrete signal) is phase/omega... i.e. the constant. More generally,
% this constant sample delay for a n-point averaging filter will be equal
% to (n-1)/2.
%So for this signal, the delay is (n - 3) and from the magnitude response,
% we can write the general form of:
%
y[n] = 1.8 * 0.374 cos(0.2*pi*((n-3)-3))
%
y[n] = 0.6732*cos(0.2*pi*(n-6))
%% Part E - Null Frequencies
% The output signal is zero (null) when the seven point averager averages
%over exactly an integer number of periods (i.e.) the averaging starts and
% finishes at the beginning and end of a period. So this means frequencies
% equal to k times 1/n where the filter is an n-point averaging filter and
% k is an integer. However, because of the domain of the discrete time
% spectrum (between pi and negative pi), this means that it is only
% frequencies (in Hz) between 1/n and pi/2 of the form k/n. In terms of
% normalized frequency, these values are 2/7*pi, 4/7*pi, and 6/7*pi.
%% Part F - Design nulling filter to null norm freq of 0.4*pi
% 0.4*pi = 2k/n*pi where k is an integer and n it is an n-point averaging
% filter. 2k/n = 0.4 --> 2k/n = 2/5 --> n = 5
% so the filter is:
% sin(5/2*n) / (5*sin(n/2))