18-290 Signals and Systems Profs. Byron Yu and Pulkit Grover Fall 2017 Homework 5 Solutions Part One 1. (18 points) For each of the following impulse responses, determine whether the corresponding LTI system is (i) stable, (ii) causal, and (iii) memoryless. Justify your answers. (a) h[n] = ( 1) n δ[n] (b) h(t) = e t u(1 t) (c) h[n] = δ[n 2k] k=0 (d) h(t) = sin( π t)u(t + 1) 2 Solution: (a) (b) (c) (d) i. Stable. n= h[n] = 1. ii. Causal. n < 0 h[n] = 0. iii. Memoryless. h[n] = δ[n]. i. Stable. h(t) dt = 1 et dt = e. ii. Not causal. h( 1) = 1 e. iii. Not memoryless. h(t) is not of the form cδ(t). i. Not stable. h[n] is 1 for all non-negative even values of n, so ii. Causal. n < 0 h[n] = 0. iii. Not memoryless. h[n] is not of the form cδ[n]. i. Not stable. h(t) dt = 1 sin( π 2 t) dt =. ii. Not causal. h( 1) = 1. iii. Not memoryless. h(t) is not of the form cδ(t). n= h[n] =.
2 Homework 5 Solutions 2. (16 points) Evaluate and plot the step response for the LTI system represented by each the following impulse responses: (a) h[n] = u[n + 1] u[n 1] (b) h(t) = e t Solution: (a) s[n] = n h[k] = n k= k= i. Case 1: n < 1 s[n] = 0. ii. Case 2: 1 n < 1 s[n] = iii. Case 3: n 1 s[n] = 2. u[k + 1] u[k 1] n k= 1 1 = n + 2. s[n] 4 3 2 1 4 3 2 1 1 2 3 4 1 2 n (b) s(t) = t h(τ)dτ = t e τ dτ i. Case 1: t < 0 s(t) = t eτ dτ = e t ii. Case 2: t 0 s(t) = 0 eτ dτ + t 0 e τ dτ = 2 e t
Homework 5 Solutions 3 s(t) 2 1 4 3 2 1 1 2 3 4 t 1
4 Homework 5 Solutions 3. (10 points) Let h 1 [n], h 2 [n], h 3 [n], h 4 [n] and h 5 [n] be impulse responses of LTI systems. Evaluate the effective impulse response of the following systems: (a) h 4 [n] x[n] h 1 [n] h 2 [n] + h 5 [n] + y[n] h 3 [n] (b) x[n] h 1 [n] h 2 [n] + h 5 [n] + y[n] h 3 [n] h 4 [n] Solution: (a) h[n] = h 1 [n] (h 4 [n] + (h 2 [n] + h 3 [n]) h 5 [n]) (b) h[n] = h 1 [n] ( (h 2 [n] + h 3 [n]) h 5 [n] + h 3 [n] h 4 [n] )
Homework 5 Solutions 5 4. (10 points, vishwa) Let h 1 [n], h 2 [n], h 3 [n], h 4 [n] and h 5 [n] be impulse responses of LTI systems. Draw an interconnected system for each of the following impulse responses: (a) h[n] = (h 1 [n] h 2 [n] + h 3 [n] h 4 [n]) h 5 [n] (b) h[n] = h 1 [n] h 2 [n] + h 3 [n] + h 4 [n] h 5 [n] Solution: (a) x[n] h 1 [n] h 2 [n] + h 5 [n] y[n] h 3 [n] h 4 [n] (b) h 1 [n] h 2 [n] x[n] h 3 [n] + y[n] h 4 [n] h 5 [n]
6 Homework 5 Solutions 5. (20 points) (a) Find the frequency response H(e jω ) of the discrete-time LTI system whose impulse response is h[n] = 1 u[n]. Plot the magnitude response H(e jω ) and phase 2 n response H(e jω ). (b) Find the frequency response H(jω) of the continuous-time LTI system whose impulse response is h(t) = u(t) u(t 1). Plot the magnitude response H(jω) and phase response H(jω). Solution: (a) Let us compute the frequency response of the signal. Thus, H(e jω ) = = k= 1 1 1 2e jω = = 1 1 2e jω h[k]e jωk = 5 ejω +e jω 4 2 H(e jω ) 2 = 1 1 1 2e jω = 1 2 k u[k]e jωk = k= ( 1 1 ) 2e jω 1 1 2e jω (4 2 cos Ω) 2j sin Ω 5 4 cos Ω k=0 ( ) 2 ( ) 2 4 2 cos Ω 2 sin Ω + 5 4 cos Ω 5 4 cos Ω = 16 + 4 cos2 (Ω) 16 cos Ω + 4 sin 2 (Ω) (5 4 cos Ω) 2 4 = (5 4 cos Ω) Or, H(e jω ) = 2 5 4 cos Ω. And H(e jω ) = arctan 2 sin Ω. 4 2 cos Ω 1 (2e jω ) k H(e jω ) 2 H(e jω ) 1 1 π 0 Ω π π 0 Ω π 1
Homework 5 Solutions 7 (b) Let us compute the frequency response of the signal. Thus, H(jω) = [ e jωt = jω h(t)e jωt dt = ] t=1 t=0 1 0 = e jω 1 jω = j (cos(ω) j sin(ω) 1) ω = sin(ω) ω + j cos(ω) 1 ω e jωt dt = j ω (e jω 1) ( ) 2 ( ) 2 sin(ω) cos(ω) 1 H(jω) 2 = + = sin2 (ω) + cos 2 (ω) 2 cos(ω) + 1 ω ω ω 2 = 2(1 cos(ω)) ω 2 = 4 sin2 (ω/2) ω 2 Or, H(jω) = 2 sin(ω/2) and H(jω) = arctan cos(ω) 1. ω sin(ω) H(jω) 2 H(e jω ) 2 1 1 0 ω 4π 3π 2π π π 2π 3π 4π 1 0 ω 4π 3π 2π π π 2π 3π 4π 2
8 Homework 5 Solutions Part Two 6. (12 points) In this problem, we will use convolution to deconstruct and approximately reconstruct a signal in MATLAB. Please download hw5 matlab.zip from Canvas or the course website (http://ece.cmu.edu/~ece290/f17/homeworks/hw5_ matlab.zip). Unzip it in your working directory, and you will find two files hw5 q1.mat and view frequencies.m. hw5 q1.mat contains a signal, called sig, as well as the impulse response of 3 filters IR1,IR2 and IR3 to process the signal with. Also included in hw5 q1.mat is the parameter fs, which is the sampling frequency of sig and is equal to 44,100 (sig is a sound signal recorded at a sampling frequency of at 44.1 khz). view frequencies.m takes in an impulse response and the sampling frequency, and outputs the frequency response. The built-in MATLAB function sound(signal, fs) will play back the sound signal. (a) Observe the frequency response of the three filters using view frequencies.m using the code below. Submit: Which one is a low-pass filter, high-pass filter and band-pass filter? (No plots or code submission needed.) clear all load (' hw5_q1. mat ') % load data into the workspace % Plot one impluse response as an example [f,amp ] = view_frequencies (IR1,fs ); plot (f,amp,' Linewidth ',2) title (' Frequency response of IR1 ') xlabel ('f(hz )') ylabel (' H(f) ') (b) Convolve each of the three impulse responses separately with the signal sig. Listen to sig and listen to each of the filtered outputs. Submit: Your code. And based only on what you hear, describe how does the sound of each of the 3 filtered outputs differ from that of sig in 1 to 2 sentences. (c) view frequencies(sig,fs) can also examine the frequency components contained in a signal, sig. Generally speaking, songs of David Bowie contain more low frequency components than Maroon 5 songs. So from what you hear in Problem 6b, you should have an expectation of what kinds of frequency components are contained in each filtered output. Now, let us verify that. Plot the frequency components of sig, as well as that of the 3 filtered outputs using view frequencies(sig,fs). Submit: The 4 plots with appropriate labels and your code. Since you know the type of filter corresponding to each impulse response in Problem 6a, label the
Homework 5 Solutions 9 plots as Convolution with Low Pass, etc. Do not use labels such as Convolution with IR1,etc. Explain how each filter modifies the frequency components of sig. (d) We separated the signal into three components, i.e, we deconstructed it into 3 bands based on frequency: low, middle, and high frequencies. Now, let us see what happens if we put the 3 components back together. Add the three convolved signals together, plot the signal, and listen to the output. Submit: The plots of the original signal and the reconstructed signal. Do the plots look exactly the same? Do the signals sound similar or different? Solution: (a) IR1 is high pass, IR2 is low pass and IR3 is band pass. (b) The three filters cause the output to only have low, medium, and high frequencies. The code is in part (c) (c) Students should see what they heard in part (b); that only certain frequencies are retained after each filtering operation (the same frequencies present in the filters themselves). Frequencies not present in the filter s frequency response do not appear in the output when the original sound signal is passed through. % view the frequency components of the original signal [f,amp ] = view_frequencies (sig,fs ); figure ; plot (f,amp,' Linewidth ',2) title (' The original signal ') xlabel ('f(hz )') ylabel (' X(f) ') % view the filtered signal high_r = conv (sig, IR1 ); % change the second input to IR2, IR3 for other filters soundsc ( high_r,fs) [f,amp ] = view_frequencies ( high_r,fs ); figure ; plot (f,amp,' Linewidth ',2) title (' Convolution with high pass ') xlabel ('f(hz )') ylabel (' X(f) ')
10 Homework 5 Solutions
Homework 5 Solutions 11 (d) The reconstructed signal should sound similar to the original, but may have a tinny sound to it. They will observe that the reconstructed signal once again has low and high pitches/frequencies. recover_sig = band_r + low_r + high_r ; soundsc ( recover_sig,fs)
12 Homework 5 Solutions plot ( sig ) title (' The original signal ') figure plot ( recover_sig (501:1000500)) title (' The recovered signal ')
Homework 5 Solutions 13 7. (13 points) Convolutions are also defined for 2D signals, such as grayscale images. In this problem we will explore image filtering based on 2D convolutions. Consider a grayscale image I(x, y), which is a discrete 2D signal, and a discrete 2D impulse response h(x, y). A discrete 2D convolution is defined as: J(x, y) = W U w= W u= U I(x u, y w)h(u, w) The impulse response h is defined on the 2D interval { U u U, W w W }, which is represented in a matrix of size (2U + 1) (2W + 1). (a) (4 points) Gaussian blur impulse response We will experiment with the Gaussian blur impulse response. In the 1D case, the Gaussian impulse response smooths out the signal and has the following form h(t) = e t2 2σ 2, where σ controls the degree of smoothing. In 2D, we can smooth in both directions. If we smooth the same amount in all directions, the impulse response will have the following expression: h(u, w) = e ( u2 +w 2 2σ 2 ) Our impulse response has a finite size of (2U + 1) (2W + 1) since it is easier to implement in Matlab. Below is the function to build a symmetric 2D Guassian impulse response for a given size and given σ. function h = gaussian2d ( sigma, U, W) %% GAUSSIAN2D generates a symmetric normalized 2 D Gauss. % of size is [2* U +1 x 2* W +1] h = zeros (2* W+1, 2*U +1); for y = 1 : 2* W +1 for x = 1 : 2* U + 1 w = y - W - 1; u = x - U - 1; h(y, x) = exp (-(w^2 + u ^2) /(2* sigma ^2)); end end Using this function, generate three impulse responses for three combinations of parameters (σ, U, W ) = (1, 5, 5), (5, 5, 5), and (5, 10, 10). Plot each of them using Matlab s function surf(h), and submit the three resulting plots. (b) (2 points) Normalization Our impulse response has to be normalized so that its values sum up to one. Change the function gaussian2d appropriately. The result should satisfy:
14 Homework 5 Solutions h = gaussian2d ( sigma, U, W); assert ( sum (h (:)) == 1); % summing up all the values Submit the new function. Also explain why this normalization is necessary for image blur. (c) (4 points) Convolution We will run experiments on saturn.png image, which is built in to Matlab. Load it and transform the image to grayscale: I = I = imread (' saturn.png '); rgb2gray (I); To display the image in a figure, use Matlab command imshow: imshow (I); Instead of implementing our own 2D convolution function, we will use Matlab function conv2 for this exercise. Just like with 1D convolution, we always need to pay attention to the boundary conditions, i.e. how to treat pixels near image edges. Matlab offers several different options. In this exercise we will use the one named same, which does not change the size of the input image: I = double (I); I2 = conv2 (I, h, 'same '); imshow ( uint8 (I2 )); Create two impulse responses with different values of σ, i.e (σ, U, W ) = (3, 9, 9) and (10, 30, 30). Use conv2 to blur the saturn image and submit two output blurred images. You can use function imwrite(uint8(i2), filepath ) to save an image. Function uint8 transforms floating point values into integers in range [0, 255]. Does the level of blurring increase or decrease with higher values of σ? (d) (3 points) Directional blur Instead of blurring uniformly, we can perform a directional blur. To blur an image in the X-direction, we need to create an impulse response, such that its values change only in the X-direction. Fortunately, we can accomplish this without making any changes to the gaussian2d function! The following code generates an impulse response with σ= 10. Note that W is set to 0, which means the impulse response is of size (2U + 1) 1. That is, the impulse response only exists along the X-direction. h_x = gaussian2d (10, 30, 0);
Homework 5 Solutions 15 Perform convolution of the saturn image with h x and submit the blurred image. How can you tell the result is blurred only along the X-direction? Solution: (a) Three figures shown below:
16 Homework 5 Solutions (b) The blur must have no effect if applied to a one-colored gray image because there is nothing to blur. Let image intensity be I 0. Then convolution operation applied at every pixel can be written as: W U w= W u= U I(x u, y w)h(u, w) = W U w= W u= U W U w= W u= U h(u, w) = 1 I 0 h(u, w) = I 0 function h = gaussian2d ( sigma, U, W) %% GAUSSIAN2D generates a symmetric normalized 2 D Gauss. % of size is X x Y = [2* U +1 x 2* W +1] h = zeros (2* W+1, 2*U +1); for y = 1 : 2* W +1 for x = 1 : 2* U + 1 w = y - W - 1; u = x - U - 1; h(y, x) = exp (-(w^2 + u ^2) /(2* sigma ^2)); end end h = h / sum (h (:));
Homework 5 Solutions 17 (c) As σ increases, blurring increases. (3, 9, 9) (10, 30, 30)
18 Homework 5 Solutions (d) One can notice that Saturns moons are blurred only in the X-direction.
Homework 5 Solutions 19
Time Taken 20 Homework 5 Solutions Common Mistakes Problem 5 Wrong label axes: instead of expressing an angle in pi or or other scale, labeled with simple numbers (1,2,3.) (I didnt penalize though). For part (b) the phase plot was wrong for everyone.