#๐ Smooth a wave using python, only use matplotlib.pyplot to show graph
35 messages ยท Page 1 of 1 (latest)
@unreal veldt
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.
just loop 200 times?
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
I mean you seem to have sound logic in your pseudo python code already
wave_input = [ ... ]
for _ in range(200):
new_wave = ... # smoothed `wave_input`
wave_input = new_wave
print(wave_input)
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
yeah this was part of my code
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
notice something different between my code and yours?
wave_input = new_wave ?
yes
so every iteration it will rewrite the result to wave_input
then smooth that result again, and write the more smooth result to wave_input again...
repeat 200 times
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
new_wave[i] = wave[i-1] * 0.2 + wave[i]*0.6 + wave[i+1]*0.2
might have problems when you do this on the first element i == 0 or the last element i == len(wave)-1
in fact I'm a little surprised you didn't get any errors?
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
yeah i will get index error, for which i used
wave_input[i] = 0 + new_wave[i] * 0.6 + new_wave[i+1] * 0.2```
the above formula takes weight of 0.2 from previous and the next element, and adds back remainin 0.6 of the current element
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
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
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?
yeah its part of my assignment ๐
i don't expect the answers though but i want some second eyes to give me another perspective
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.