question.
10. Complete the function median_filter that takes in two parameters, y and W. y is a list of y coordinates which are numbers. • W is a positive integer. • The function returns a list where the ith element in y is amapped to a median value, as a float, over a sliding window containing y specifically. median(y[i-W],...,y[i-1], y, y[i+1], ...,y[i+W]) if i - W > Oand i + W < len(y) y {median(y[0],y[1],...,y[i-1], y, y[i+1],...,y[i+W]) if i - W < Oand i + W < len(y) median(y[i-W],...,y[i-1], y, y[i+1], ...,y[n-2], y[n-1]) if i-W > Oand i +W > len(y) The basic idea of the median filter is that it replaces the ith point with the median of the points in a window of size 2xW around the ith point. (Note: The window has 2xw items in the first case above: the window is smaller for the second and third cases.) An example function call on an input list of 7 items is given below. >>> y = [-1, 6, 7, -2, 0, 8, 13] >>> 2 median_filter(y, 1) = >>> Z [2.5, 6.0, 6.0, 0.0, 0.0, 8.0, 10.5] Below is the assignments that median_filter(y, 1) makes to each slot in z. z [0] = median ([-1, 6]) z[1] = median ([-1, 6, 7]) z [2] = median ([6, 7, -2]) z [3] = median ( [7, -2, 0]) z [4] = median ([-2, 0, 8]) z [5] = median ([0, 8, 13]) z[6] = median ([8, 13]) The plot below shows a longer list y of 50 elements (daily Air Quality Index measurements) plotted against index values 0, 1, 2, ..., 49 as blue solid circles. The plot also shows the items in the list median_filter(y, 1) plotted against the index values 0, 1, 2, ..., 49 as semitransparent brown solid circles. We can see that the median filter “removes" unusual or outlying observations.
7 6 5 4 3 2 1 0 10 20 30 40 50 10 The plot below shows the same list y of 50 elements (daily Air Quality Index measurements) plotted against index values 0, 1, 2, ..., 49 as blue solid circles. The plot also shows the items in the list median-filter(y, 6) plotted against the index values 0, 1, 2, ..., 49 as semitransparent brown solid circles. We can see that the broader median filter smooths the data to a much greater extent. 7 6 5 4 3 2 1 0 10 20 50 Note: Air Quality Index data from https://archive.ics.uci.edu/ml/datasets/Air+ Quality
Using Python to solve the Using Python to solve the question.
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am