# Name: *** Enter your name here *** # # Student-ID: *** Enter your student ID here *** # # Rename the file to 01.py # # Submit your solutions via moodle # import numpy as np import scipy.signal as sig import matplotlib.pylab as plt # Please do not change the code above this line # Exercise 1: Fourier representation of sinusoidal signals # def exercise1(): # 1D arrays f = np.array([0, 1, 2, 3, 2, 1, 0]) g = np.array([1, 2, 3]) h = np.array([1, -2, 1]) # 2D arrays F = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 1, 2, 1, 0, 0], [0, 1, 2, 2, 2, 1, 0], [0, 0, 1, 2, 1, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]) G = np.array([[1, 2, 3], [2, 3, 0], [3, 0, 0]]) H = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]]) # Please do not change the code above this line # Task 1B: compute the (full) convolution with `np.convolve` (for 1D arrays) # and `scipy.signal.convolve2d` (for 2D arrays) # *** Insert your Python code here *** # Task 1C: Compute `f o g` (where `o` denotes the convolution) by applying # the convolution theorem # zero-pad the filter `g` so as to match the size of the signal `f` # # *** Insert your Python code here *** # move the center of the padded filter to the start of the array # # *** Insert your Python code here *** # map the signal and modified filter to the frequency domain # # *** Insert your Python code here *** # multiply the (transformed) arrays element-wise # # *** Insert your Python code here *** # map the result back to the spatial domain # # *** Insert your Python code here *** # compare the result with the result obtained in 1B # # *** Insert your Python code here *** # Task 1D: Compute `F o G` by applying the convolution theorem. Proceed as # in 1C by using the 2D versions of fft and ifft # *** Insert your Python code here *** # Exercise 2: Denoising with a sliding average # def exercise2(): # close all figures plt.close('all') # generation of a noisy signal # fix PRNG seed np.random.seed(42) # unperturbed signal N = 1000 x = np.linspace(0, 5*np.pi, N, endpoint=False) signal = np.sin(x) + np.sin(2*x) + np.cos(5*x) # generate noise with desired SNR snr = 5. sigma = np.sqrt(np.var(signal) / snr) noise = sigma * np.random.randn(N) # perturbed signal y = signal + noise # please do not change the code above this line # Task 2A: Compute the moving average of `y` where a data point is replaced # by the average of the previous 10 data points, the data point itself and # the following 10 data points. # # *** Insert your Python code here *** # Task 2B: Compute the moving average of `y` by using the fact that a # sliding average can be expressed as the convolution of the signal with a # rectangular pulse. # # *** Insert your Python code here *** # Task C: Plot the results obtained in 2A and 2B and compare them. # # *** Insert your Python code here *** # Exercise 3: Filtering an image # def exercise3(): # close all figures plt.close('all') # Task 3A: Load 'noisycells.tif' with plt.imread and convert its datatype # to double. Filter the image by averaging all pixels inside a patch of # size 5x5. # # *** Insert your Python code here *** # Task 3B. Filter the image by using a convolution (sig.convolve2d); see # also Task 2B for a 1D version of the same task. # # *** Insert your Python code here *** # Task 3C. Generate a binary image by thresholding the image from 3A (or # 3B) at a minimum intensity of 50. # # *** Insert your Python code here *** # Task 3D. Apply filter `H` from Exercise 1 to the image obtained in 3C. # Binarize the resulting image by setting all intensities different from # zero to `True` and all pixels with intensity zero to `False` # # *** Insert your Python code here *** # Task 3E. Plot all images # # *** Insert your Python code here *** # Test code - you shouldn't change this part of the template if __name__ == '__main__': exercises = {'1': (exercise1, ), '2': (exercise2, ), '3': (exercise3, ), } choice = input('Please enter the number of the exercise: ').upper() if choice not in exercises: print('Please choose among {0}'.format( list(exercises.keys()))) else: func, *args = exercises[choice] print('Task {0}, running "{1}"'.format( choice, func.__name__)) result = func(*args) if result is not None: print('Result:', result)