Question
Python language question explanation about 1-2-1 method: Often data measured in the real word is noisy. So we need to apply some kind of filter
Python language question
explanation about 1-2-1 method:
Often data measured in the real word is noisy. So we need to apply some kind of filter to clean up the data. In this question we are going to write a function that applies the "1-2-1" filter. The "1-2-1" filter maps each point of data to the average of itself twice and its neighbors
data.txt for plotting:
0.000000000000000000e+00 1.539222746762192351e+01 4.399208505782237211e-03 -1.307295274049445677e+00 8.015716473555293931e+00 -1.281714644970376682e+00 3.688694036249841268e+00 1.664335986527485645e+01 1.461770195531442162e+00 -1.873839914438522491e+00 1.927609380497408864e+01 1.493254677558604726e+01 3.076879347054811475e+00 1.124908693577696894e+01 5.949357911560769097e+00 1.005042101449891589e+00 2.039644049876629950e+01 1.834909587412036913e+01 -1.074156825573327367e+00 8.888370894941111544e+00 1.654362868616317428e+01 3.605454012563596322e+00 7.987292411134694348e+00 1.067904374377847532e+01 -3.498774922564585665e+00 7.088708253288592331e+00 2.088948435052724051e+01 1.328896772892835809e+00 -6.353232791922378553e+00 5.639147652116234255e+00 -1.192071056794827388e+00 -1.915114930882899191e+00 9.594172773418305766e+00 -4.292157342159281797e+00 -1.247298720707734709e+01 6.253180563444102447e+00 1.786144289703208266e+00 -1.543743855349520544e+01 -7.258559596300703909e+00 -6.035085465259172999e+00 -1.286303979035413114e+01 1.465580196076311736e+00 -1.463882024378975188e-01 -2.182452296980252981e+01 -1.349550891926515561e+01 -8.841813446255777365e-01 -1.374176378932378384e+01 -1.240309766893482468e+01 -4.594681244888592531e+00 -1.582070268241283806e+01 -9.558644976400760385e+00 4.443091492419501520e+00 -1.343849520553049359e+01 -2.173027737949170302e+01 -3.345469878037349076e+00 -4.065390086059913699e+00 -8.327365943095225376e+00 2.890898551841775799e+00 -6.121544899294349307e+00 -1.498491897493611447e+01 4.487798994719858214e+00 5.063435655048943218e+00 -1.202120097329023629e+01 -1.568433178528040761e+00 7.309877807092933111e+00 -4.294003751091333143e-01 8.949546128482793961e+00 1.031917518412056722e+01 -8.845164377679836321e+00 1.391358809722555989e-01 1.837312053657823796e+01 6.523441186121239355e+00 3.615415133919212209e+00 1.439712817406439704e+01 5.560837547436998207e+00 6.549833621981648868e+00 2.010753573868939270e+01 5.488203947735993182e+00 -3.762841953933081740e+00 1.687352997739518656e+01 1.809214595290582395e+01 6.332702009989967351e+00 1.323832941611767922e+01 7.613136623600534669e+00 -2.979974635554434315e+00 1.384332369370619098e+01 1.645788760994372879e+01 -3.412125786040092201e+00 3.540437414542892292e+00 1.431337038468104161e+01 2.083464196385731437e+00 3.360636293093010707e+00 6.242025448671627785e+00 -1.063096622503028499e+01 -4.428419173417313282e+00 1.413562963820093188e+01 -1.342851029215563763e-01 -1.052082370566100877e+01 4.028235467571477102e-01 -6.186863585322647907e+00 -1.042451375545435965e+01 2.014277774884828176e+00 -8.694129188238143513e+00 -1.966864074388715977e+01 -1.357989442436782213e-01 1.808756301181649206e+00 -1.590920761755949542e+01 -1.129189679047781780e+01 -9.930984215766564205e+00 -1.882860132174410950e+01 -4.158933932544203849e+00 1.312946300246414566e+00 -1.891679614481802929e+01 -1.419945714149302418e+01 6.909020039275368674e-01 -1.086093137743757708e+01 -1.270890261390108833e+01 -3.860746914012376685e+00 -1.400216176566538806e+01 -9.122036451212636621e+00 1.010777050687414480e+01 -2.935493415729921729e+00 -1.599390886168231241e+01 -7.962691489345312679e-01 1.275445187988433204e-02 -5.435467917225770940e+00 7.474293529559227878e+00 2.770726461765860016e+00 -8.362645827869933157e+00 9.853655444894560134e+00 1.463853192704128503e+01 -4.584615332301432034e+00 4.668324022828729269e-01
Often in electrical engineering we need to measure electrical or radio signals. In the real world these signals are not perfect waves and can contain lots of noise. The following plot shows some data from a noisy signal plotted using matplotlib: Noisy signal 15 10 5 0 Amplitude -5 -10 -15 0.0 2.5 5.0 7.5 12.5 15.0 17.5 10.0 Time (ms) We would like to improve this signal by cleaning it up. One way to do this is to apply the "1-2-1" filter from the previous question. After applying the "1-2-1" filter once to the same data from before before we get the following plot: 1-2-1 filtered signal (n=1) 15 10 5 0 Amplitude -5 -10 -15 0.0 2.5 5.0 7.5 12.5 15.0 17.5 10.0 Time (ms) It's a little better, but still pretty noisy right? Well let's see what happens after we apply the "1-2-1" filter 5 times. 1-2-1 filtered signal (n=5) 10 5 Amplitude W -10 0.0 2.5 5.0 7.5 12.5 15.0 17.5 10.0 Time (ms) That's clearly an improvement, but still a bit rough. Finally we will look at what happens after the "1-2-1" filter has been applied 20 times. 1-2-1 filtered signal (n=20) 10.0 7.5 5.0 2.5 1 0.0 v -2.5 -5.0 -7.5 -10.0 0.0 5.0 7.5 12.5 15.0 17.5 10.0 Time (ms) We get a nice smooth looking wave function representing our signal. Write a function plot_filtered_signal(filename, n) that takes the name of a file containing data for a noisy signal and plots a smoothed version of the signal after applying the "1-2-1" filter n times. The file will contain a list of data samples each 0.1 ms apart. There is one sample (a floating-point value) on each line of the file. You should label your plot as is shown in the image above. Notes: To apply the filter n times, you should apply it once to get a new signal, then apply it again to get another new signal, and so on, for a total of n times. It is safest to create a new array on each iteration; attempting to perform the filtering "in-place" on the same array will probably give you the wrong results. The last sample in the array has an x-value of (len(array) - 1) / 10 ms, not len(array) / 10 ms. You should call the plot function with an array of time values and an array of y values. y There are more efficient ways of heavily smoothing a signal than to apply this simple filter a large number of times. You'll learn about these if you do a signal processing course in your degreeStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered 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