Home>
Error message
I get an error like the following, and it is said that it has been divided by zero, but I am in trouble because I do not know where it is dividing by zero.
Traceback (most recent call last):
File "/Users/matsumura/PycharmProjects/wave/pinknoise_generater.py", line 29, in<module>
a, b = synth_pinknoise (duration = 10.0, samprate = 48000)
File "/Users/matsumura/PycharmProjects/wave/pinknoise_generater.py", line 17, in synth_pinknoise
t = np.arange (start = 0, stop = duration, step = (int (1/samprate)))
ZeroDivisionError: float division by zero
Applicable source code
# coding: UTF8
# Required libraries
import math
import numpy as np
import matplotlib
matplotlib.use ('TkAgg')
import matplotlib.pyplot as plt
# Define pink noise function
def synth_pinknoise (duration, samprate):
"" "
Generate pinknoise using Voss algorithm
http://www.firstpr.com.au/dsp/pinknoise/
"" "
f_low = 10 # lowest frequency to keep pink (Hz)
levels = math.ceil (np.log2 (samprate/f_low))
t = np.arange (start = 0, stop = duration, step = 1/samprate)
out = np.zeros (len (t))
x = np.random.normal (size = levels)
for n in range (math.ceil (samprate * duration)):
for m in range (levels):
if n% 2 ** (m + 1) == 0:
x [m] = np.random.normal ()
out [n] = np.random.normal () + np.sum (x)
return (out/max (abs (out)), t)
# Usage (Return values are x: Pink noise signal, t: Time of each sample)
a, b = synth_pinknoise (duration = 10.0, samprate = 48000)
plt.plot (a, b)
plt.show ()

Answer # 1

Answer # 2
Look at the error message because it specifies the line.
and
t = np.arange (start = 0, stop = duration, step = (int (1/samprate)))
Consider how many steps this will be
Related articles
 python 3x  i want to divide the process when it is err as a result of command execution in python3 subprocess
 python  i want to divide a binary number into two, store it in a list, and represent it in a different decimal number
 python  i want to divide the image into grids and find the hsv value
 python  i want to divide the processing by conditional branching for the data frame
 python  i want to divide the list of the amount of data that does not fit in the memory according to the conditions
Related questions
 python  i want to find the average hsv value for each divided image
 python  background program by sharley method
 python  i want to load graph data created with pyplot as image data without saving it
 python  i want to find the intersection of the lines of the least squares method when the button is pressed
 i want to see the change of one element of a recurrence formula including a python matrix
 python  i want to create a binary mask by comparing two elements and setting the smaller one to 0
 python  i want to display a graph of matplotlib on tkinter and enable mouse operation in canvas
 in [python] print (), if a scatter plot is displayed using a onedimensional array that has the intended values, the distributio
 python  numpy import
 python  i want to search for a specific value in a row specified by a twodimensional array
step = (int (1/samprate))
will give 0 tostep
, so insidenp.arange
Will cause a division by zero error. There is an internal calculation that divides bystep
(you can see it immediately when you imagine how to produce an arange result).