In: Computer Science
Write a function ‘filter_wave(wave,n)’ and this will produce a new wave which is a smoothed version of the input ‘wave’ adhering to the following rules. The input parameter n will be explained later in part 2. • In the new wave, for every position i, it is the weighted sum of three positions of the original wave. More preciously, the new wave value in position i will be equal to new_wave[i] = wave[i-1] * 0.2 + wave[i]*0.6 + wave[i+1]*0.2 • Let len(wave) be L. The above calculation requires access to wave[-1] and wave[L] which do NOT exist in the original wave. You may assume that both wave[-1] and wave[L] are 0. • You should NOT modify the original wave input • And all the number in the new wave will be integers. You can simple use the function int() to convert any number into an integer. • Your function should return the new wave as a list.
Finally, modify the function ‘filter_wave(wave,n)’ for n ≥ 0. And this will repeat the filtering n times to the wave accumulatively and produce an even smoother wave. You still need to adhere to the rules mentioned in Part 1. Here is the expected wave for filter_wave(original_wave_sample,10)
I used Python for this problem. The following is the code-->
def filer_wave(wave,n):
//fiter_wave function declaration which takes two parameters wave
and n
i=1
L=len(wave) // variable L contains the length of the wave
list
new_wave=wave // here we are
declaring a list new_wave, and initializing it with wave list.
while(i<=n): //We are looping n times to
modify new_wave n times so that the wave is filtered n times.
//This was explained in the second part of the
question.
j=0
while(j<L): //Now we are looping L times to modify each
index of the wave list and store the filtered value //in
new_wave
if j==0 and L>1: // This condition
checks for first element of wave list and if the length of wave
list greater than1
s=(int)(new_wave[j]*0.6+new_wave[j+1]*0.2)
elif j==0 and L==1: //This condition checks for
first element of wave list and if the length of wave list equal to
one
s=(int)(new_wave[j]*0.6)
elif j==L-1: //This condition checks for last
element of wave list
s=(int)(new_wave[j-1]*0.2+new_wave[j]*0.6)
else: // This condition works for element in
between first and last element of wave list
s=(int)(new_wave[j-1]*0.2+new_wave[j]*0.6+new_wave[j+1]*0.2)
// We are using the required expression from // equation. And
converting it to Integer
new_wave[j]=s //We have computed
values for each condition in s. Here we are storing s in correct
position of // new_wave list
j+=1 //Iterating inner loop
i+=1 //Iterating outer loop
return new_wave //Returning new_wave list as
required