#๐Ÿ”’ Smooth a wave using python, only use matplotlib.pyplot to show graph

35 messages ยท Page 1 of 1 (latest)

unreal veldt
#
  #my task is to create a new_wave = [0,0,0,0,0,0]
  #and use the following formula to smoothen it
  #new_wave[i] = wave[i-1] * 0.2 + wave[i]*0.6 + wave[i+1]*0.2

  #how can i do this for 200 times such that 
  #new_wave will now be = [2.190, 4.354, 6.463, 8.492, 10.416, 12.211]
twin pathBOT
#

@unreal veldt

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

unreal veldt
#

the numbers won't add up to 2.190 if i multiply it 200 times... i've been thinking what's the smoothening logic behind this

#

i felt like I have to use a nested list to contain 200 iterations of the wave such that [[1,2,3,4],[x1,x2,x3,x4],y1,y2,y3,y4]] and use the previous array to generate the new array

rustic relic
#

not exactly sure what the problem is
unless by "not adding up to 2.190" you mean float errors

#

in that case you could choose to tank performance and use fractions.Fraction if you want

unreal veldt
#

let me show a snippet

#
    new_wave = []
    for i in range(0,len(wave)):
        new_wave.insert(i,0) #insert the list with 0s so i can call its index later

    for y in range(0,times-1):
        for i in range(0,len(new_wave)):
             new_wave[i] = wave[i-1] * 0.2 + wave[i]*0.6 + wave[i+1]*0.2```
#

if 3 * 0.2 is already 0.6, 200 times will make it 120

rustic relic
unreal veldt
#

wave_input = new_wave ?

rustic relic
unreal veldt
#

so every iteration it will rewrite the result to wave_input

rustic relic
unreal veldt
#

hmmmm

#

let me try it

#

[1.9782656953940232, 3.926866783344744, 5.822791923790608, 7.644162536675749, 9.370646328057173, 10.983832948161773 i got something like 1.97 which is still far off from the 2.190

#

need to further troubleshoot it

rustic relic
#

in fact I'm a little surprised you didn't get any errors?

primal laurel
#

Thinking about it some more I don't know how simply smoothing out the wave would cause the first element to ever not be 0, or the last one to not be 24

unreal veldt
unreal veldt
primal laurel
#

I suppose why I'm confused is that's not really smooth as much as it is leveling out, I think if you ran your code with enough iterations you might get just a straight horizontal line

unreal veldt
#

yes in that case 10,000 times will be [0.005394815860210735, 0.010782305342016428, 0.01615662151607011, 0.021511940097755214, 0.026842468439814617, 0.03214245446251207

#

very close to 0

#

but my issue now is with the 1st element not in the right value as specified in the test case

primal laurel
#

It's hard to say how to help you because what your doing seems very custom to your situation. Why are you striving to get to those values exactly? Is this part of an assignment or just a goal for some other thing you're working on?

unreal veldt
#

yeah its part of my assignment ๐Ÿ™‚

#

i don't expect the answers though but i want some second eyes to give me another perspective

twin pathBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.