Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a new filter, called mean_filter in filters.py that replaces each sensor reading with the mean of itself and the readings on either side of
- Write a new filter, called mean_filter in filters.py that replaces each sensor reading with the mean of itself and the readings on either side of itself. For example, if you have these measurements in the middle of the list
10 13 13
then the middle reading would be replaced by 12 (since (10 + 13 + 13) / 3 = 12). Write code to generate two files, and plot the data in these files, showing the effect of applying your mean filter. We should be able to call your filter using l2 = mean_filter(l).
- Modify your code to have a variable filter width. Instead of a width of 3, add a parameter to the filter function that specifies the filter width. This parameter must be positive and odd. Test this with a variety of widths, and see what it does to the data. We should be able to call your code with l2 = mean_filter(l, 5). We should also be able to call the code with l2 = mean_filter(l), in which case the width should default to 3. Note that for both parts 1 and 2, observations where the window extends off the list should not be included. E.g. if we have a list of length 8 and our window size is 3, then the resulting list should be length 6.
- Make sure your code throws a ValueError if the user passes in a window size which is not positive and odd. Input/Output Examples:
- mean_filter([1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1]) -> [2, 2.333, 2, 1.667, 2, 2.333, 2, 1.667, 2, 2.333, 2]
Note that:
- You should write tests for all of the functions you write for this lab. Write the tests before you write the code, and put them after the if __name__ == '__main__': line. We're going to import the functions from your files for testing and, if you don't put your tests after this line (and indented to the if block), then it might confuse our testing code. Also, this if good programming practice.
- There are likely to be built-in functions for a number of the things we're asking you to do for this lab. Please don't use them. If you do, you'll get zero points for that part of the assignment, since we want you to write them from scratch (this time).
- The amplitude filter only affects sensor readings beyond a certain threshold. These sensor readings get clipped to the maximum allowed amplitude.
- When you write the variable-width mean filter, you might find array slicing useful. Also, the filtered arrays will be shorter than the input arrays. For a filter with width 3, the filtered lists will be 2 elements shorter than the original list. For a width of 5, it will be 4 shorter, and so on.
- Use Python 3.8
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started