Hi, I need some help in completing the second part of my assignment, guitar stri
ID: 3812064 • Letter: H
Question
Hi, I need some help in completing the second part of my assignment, guitar string.py. I completed the first part, ring buffer, and need to use it to complete the second part, any help?
Here is the ring_buffer.py
"""
ring_buffer.py
Models a ring buffer.
"""
import stdarray
import stdio
import sys
def create(capacity):
buff = stdarray.create1D(capacity, None)
size = 0
first = 0
last = 0
rb = [buff, size, first, last]
return rb
def capacity(rb):
"""
Return the capacity of the ring buffer.
"""
capacity = len(rb[0])
return capacity
def size(rb):
"""
Return the number of items currently in the buffer rb.
"""
items = rb[1]
return items
def is_empty(rb):
"""
Return True if the buffer rb is empty and False otherwise.
"""
if (size(rb) == 0):
return True
else:
return False
def is_full(rb):
"""
Return True if the buffer rb is full and False otherwise.
"""
if (size(rb) == capacity(rb)):
return True
else:
return False
def enqueue(rb, x):
"""
Add item x to the end of the buffer rb.
"""
rb[0][rb[3]] = x
if (rb[3] + 1) == capacity(rb):
rb[3] = 0
else:
rb[3] += 1
rb[1] += 1
def dequeue(rb):
"""
Delete and return item from the front of the buffer rb.
"""
v = None
v = rb[0][rb[2]]
if (rb[2] + 1) == capacity(rb):
rb[2] = 0
else:
rb[2] += 1
rb[1] -= 1
return v
def peek(rb):
"""
Return (but do not delete) item from the front of the buffer rb.
"""
peek_rb = rb[0][rb[2]]
return peek_rb
def _main():
"""
Test client [DO NOT EDIT].
"""
N = int(sys.argv[1])
rb = create(N)
for i in range(1, N + 1):
enqueue(rb, i)
t = dequeue(rb)
enqueue(rb, t)
stdio.writef('Size after wrap-around is %d ', size(rb))
while size(rb) >= 2:
x = dequeue(rb)
y = dequeue(rb)
enqueue(rb, x + y)
stdio.writeln(peek(rb))
if __name__ == '__main__':
_main()
----------------------------------------------------------------------------------------------------------------------------------------
And here is guitar_string.py HELP HERE
"""
guitar_string.py
Models a guitar string.
"""
import math
import random
import ring_buffer
import stdarray
import stdio
import sys
# Sampling rate.
SPS = 44100
def create(frequency):
"""
Create and return a guitar string of the given frequency, using a sampling
rate given by SPS. A guitar string is represented as a ring buffer of
of capacity N (SPS divided by frequency, rounded up to the nearest
integer), with all values initialized to 0.0.
"""
def create_from_samples(init):
"""
Create and return a guitar string whose size and initial values are given
by the list init.
"""
def pluck(string):
"""
Pluck the given guitar string by replacing the buffer with white noise.
"""
def tic(string):
"""
Advance the simulation one time step on the given guitar string by applying
the Karplus-Strong update.
"""
...
def sample(string):
"""
Return the current sample from the given guitar string.
"""
...
def _main():
"""
Test client [DO NOT EDIT].
"""
N = int(sys.argv[1])
samples = [.2, .4, .5, .3, -.2, .4, .3, .0, -.1, -.3]
test_string = create_from_samples(samples)
for t in range(N):
stdio.writef('%6d %8.4f ', t, sample(test_string))
tic(test_string)
if __name__ == '__main__':
_main()
GUIDELINES HERE
• create_from_samples(init) = 1. (Create a ring buffer whose capacity is same as the size of the given list init), 2. (Populate the ring buffer with values from init), 3. Return the ring buffer
• pluck(string) = Replace each value (dequeue followed by enqueue) in the given ring buffer with a random number from the interval [0.5, 0.5]
• tic(string) = Dequeue a value a in the given ring buffer and peek at the next value b • Enqueue the value 0.996 * 0.5 * (a + b) into the ring buffer
• sample(string) = Peek and return a value from the given ring buffer
Output for guitar_string.py should be like this
$ python guitar_string .py 25
0 0.2000
1 0.4000
2 0.5000
3 0.3000
4 -0.2000
5 0.4000
6 0.3000
7 0.0000
8 -0.1000
9 -0.3000
10 0.2988
11 0.4482
12 0.3984
13 0.0498
14 0.0996
15 0.3486
16 0.1494
17 -0.0498
18 -0.1992
19 -0.0006
20 0.3720
21 0.4216
22 0.2232
23 0.0744
24 0.2232
Explanation / Answer
guitar_string.py
"""
guitar_string.py
Models a guitar string.
"""
import math
import random
import ring_buffer
import stdarray
import stdio
import sys
# Sampling rate.
SPS = 44100
def create(frequency):
"""
Create and return a guitar string of the given frequency, using a sampling
rate given by SPS. A guitar string is represented as a ring buffer of
of capacity N (SPS divided by frequency, rounded up to the nearest
integer), with all values initialized to 0.0.
"""
string = [0.0]*int(SPS/frequency)
return string
def create_from_samples(init):
"""
Create and return a guitar string whose size and initial values are given
by the list init.
"""
gs = init
return gs
def pluck(string):
"""
Pluck the given guitar string by replacing the buffer with white noise.
"""
for x in string:
x = random.uniform(-0.5, 0.5)
def tic(string):
"""
Advance the simulation one time step on the given guitar string by applying
the Karplus-Strong update.
"""
x = string.pop(0)
y = string[0]
string.append(.996 * 0.5*(x + y))
def sample(string):
"""
Return the current sample from the given guitar string.
"""
return string[0]
def _main():
"""
Test client [DO NOT EDIT].
"""
N = int(sys.argv[1])
samples = [.2, .4, .5, .3, -.2, .4, .3, .0, -.1, -.3]
test_string = create_from_samples(samples)
for t in range(N):
stdio.writef('%6d %8.4f ', t, sample(test_string))
tic(test_string)
if __name__ == '__main__':
_main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.