Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1269

Python • Re: Gradually starting and stopping a numpy generated sine wave

$
0
0
I had to graph it to visualize it better. I'm not comfortable with my math skills so it may be off, but it seems to look ok and, when transfered to the audio generator, seems to sound better.

Code:

#!/usr/bin/python3import numpyimport matplotlib.pyplot as pltsteps      = 0.001duration   = 0.1amplitude  = 1.0        # range [0.0, 1.0]sampleRate = 8000       # integer sample rate, Hzfrequency  = 440.0      # float sine frequency, HzoneDuration = round(frequency * duration * 2, 0) / frequency / 2twoDuration = round(frequency * duration * 2 * 3, 0) / frequency / 2print ("oneDuration", oneDuration)print ("twoDuration", twoDuration)one = ( numpy.sin(    numpy.arange( sampleRate * oneDuration ) \    * frequency / sampleRate \    * numpy.pi * 2 ) ).astype( numpy.single )two = ( numpy.sin(    numpy.arange( sampleRate * twoDuration ) \    * frequency / sampleRate \    * numpy.pi * 2 ) ).astype( numpy.single )oneb = ( ( numpy.sin(    numpy.arange( sampleRate * oneDuration ) \    * 10 / sampleRate \    * numpy.pi * 2 ) ).astype( numpy.single ) ) * onea = oneb[1:int(oneb.size/4)]b = oneb[-int(oneb.size/4):-1] * -1c = two[1:int(two.size - oneb.size / 2)]twob = numpy.concatenate((a, c, b), casting="same_kind")a = oneb[1:int(oneb.size/4)]b = oneb[-int(oneb.size/4):] * -1c = one[1:int(one.size - oneb.size / 2)]oneb = numpy.concatenate((a, c, b), casting="same_kind")print("one", one.size)print("two", two.size)print("oneb", oneb.size)print("twob", twob.size)if True:    npTime = numpy.arange(start=0,                          stop=oneb.size / 1000,                          step=steps)    plt.plot(npTime,oneb,linewidth=1,label=str(frequency)+"Hz")    plt.margins(x=0, y=1)    plt.legend()if True:    npTime = numpy.arange(start=0,                          stop=twob.size/1000,                          step=steps)                              plt.plot(npTime, twob,linewidth=1,label=str(frequency)+"Hz")    plt.margins(x=0, y=1)    plt.legend()plt.show(block=True)exit(0)
Thanks to all who took a look at the question.

Statistics: Posted by tpyo kingg — Sun Mar 23, 2025 2:37 am



Viewing all articles
Browse latest Browse all 1269

Trending Articles