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  i want to divide the contents displayed from the database by user
 python  i want to divide the read image into pixels and label them
 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
Related questions
 python : Building a nonlinear regression
 python : Building a nonlinear regression
 python : Uniformly distributed elements of arrays
 python : How do I assign names to columns in an array?
 I want to output csv by combining the character string array and numerical array in Python
 python : Japanese notation in matplotlib on networkx
 Sorting rows of generated matrix (Python)
 python : x, y, and format string must not be None error
 python : Help create numpy array
 python : Need help finding the index on the Series object s
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).