In lab 2, assignment 2, you implemented a smoothing system using convolution with a box of length N and height 1/N: h₁ [
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
In lab 2, assignment 2, you implemented a smoothing system using convolution with a box of length N and height 1/N: h₁ [
In lab 2, assignment 2, you implemented a smoothing system using convolution with a box of length N and height 1/N: h₁ [n] = (u[n] - u[n — N])/N. Find the coefficients {a, b} of the linear constant coefficient difference equation (LCCDE) describing this system for N=10. Find the coefficients for the system h₁ [n] = 0.8¹u[n]
# Assignment 2- Amplitude Operations on Signals # Part A # set up relevant parameters srate = 1000 # sampling rate in Hz time = np.arange(0,2,1/srate) n = len(time) # length of the time vector # here is a base signal to work with, values of signal points chosen randomly = 10 # points for piecewise linear signal Р amp = 20 # amplitude range of base signal base = np. interp(np. linspace(0,p,n), np.arange(0,p), np. random. rand (p)*amp) # create some random noise to be added to the abve base signals noiseamp = 2 noise = noiseamp * np.random.randn(n) noisy = base + noise # add noise to the base signals to create new noisy signals # TODO: Code that solves the rest of A fig = plt.figure(1, figsize = (8,8)) plt.subplot (211) plt.ylim(0, 25) plt.plot(time, base) plt.title('original signals') plt.xlabel('sample number') plt.ylabel('base') plt.subplot(212) plt.ylim(0,25) plt.plot(time, noisy) plt.title('noisy signals') plt.xlabel('sample number') plt.ylabel('noisy') fig.tight_layout() # associated time vector that corresponds to 2 seconds
# Part B # implement the running mean filter with a for loop # TODO: Code that solves B filtsigl = noisy.copy() k = 20 for i in range(20, 1980): mean = np.mean(filtsigl[i − k : i+k]) filtsigl = mean fig2= plt.figure(2, figsize = (8,8)) plt.plot(time, noisy) plt.plot(time, filtsigl) plt.title('filtsig1') fig2.tight_layout() # Part C # implement smoothing using convolution # TODO: Code that solves C N = 2 * k + 1 hfilt = np.zeros( [N]) hfilt = hfilt + 1/N filtsig2= np.convolve(base, hfilt) fig3 = plt.figure(3, figsize = (8,8)) new_time = np.arange(0, len(filtsig2) / 1000, 1/srate) plt.plot(new_time, filtsig2) plt.plot(time, filtsig1) plt.title('filtsig2') fig3.tight_layout() #Assignment 2 hint: using a for loop, consider a "sliding window" as follows: x=np.arange (10) print('x = ',x) for k in range(0,5): | print('k=',k,', window = ',x[k:k+3]) print('etc...')