felixpatzelt / colorednoise Goto Github PK
View Code? Open in Web Editor NEWPython package to generate Gaussian (1/f)**beta noise (e.g. pink noise)
License: MIT License
Python package to generate Gaussian (1/f)**beta noise (e.g. pink noise)
License: MIT License
The code I am trying to run should print out a histogram that proves central limit theorem that when you sum of several noise sources of different distributions you get a normal distribution
import colorednoise as cnoise
noises = []
for i in range(100):
noise = 0
for n in range(3):
noise = noise + cnoise.powerlaw_psd_gaussian(1,n) # order of the noise = n (n=0:white, n=1:pink, n=2:brown)
noises.append(noise)
plt.hist(noises)
However I'm getting the following error:
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
Cell In[43], line 7
5 noise = 0
6 for n in range(3):
----> 7 noise = noise + cnoise.powerlaw_psd_gaussian(1,n) # order of the noise = n (n=0:white, n=1:pink, n=2:brown)
8 noises.append(noise)
10 plt.hist(noises)
File [c:\Users\.\AppData\Local\anaconda3\lib\site-packages\colorednoise.py:81](file:///C:/Users/./AppData/Local/anaconda3/lib/site-packages/colorednoise.py:81), in powerlaw_psd_gaussian(exponent, size, fmin, random_state)
77 samples = size[-1]
79 # Calculate Frequencies (we asume a sample rate of one)
80 # Use fft functions for real output (-> hermitian spectrum)
---> 81 f = rfftfreq(samples)
83 # Validate / normalise fmin
84 if 0 <= fmin <= 0.5:
File [c:\Users\.\AppData\Local\anaconda3\lib\site-packages\numpy\fft\helper.py:218](file:///C:/Users/./AppData/Local/anaconda3/lib/site-packages/numpy/fft/helper.py:218), in rfftfreq(n, d)
216 if not isinstance(n, integer_types):
I'm running python 3.10.9 and have used ```pip install colorednoise``` to install
217 raise ValueError("n should be an integer")
--> 218 val = 1.0/(n*d)
219 N = n//2 + 1
220 results = arange(0, N, dtype=int)
ZeroDivisionError: float division by zero
Seems like a problem in the core algorithm somewhere. Thoughts?
I'm using python 3.10.9. Installed using pip install colorednoise
Hi, and thanks for making this available.
I'm not sure if this is more of a feature request or just a question/lack of understanding on how to use the script from my part. I need to generate pink noise but with a sampling frequency of 20Hz instead of 1. If I just generate a big signal and sample it at different frequencies, the behaviour I get is quite different.
To illustrate, see the two attached figures, where I used the same random seed and generated both pink and brown noise for 30s, but sampled at 1Hz vs 100Hz. I would have expected a similar version of the signals in both cases, just better "interpolated". Instead, I see much more variance in the 100Hz case.
To fix(?) this, I thought it would be enough to simply adapt the frequency generation in line 68 of the script, as such f = rfftfreq(samples, 1/fs)
, but this seemed to make no difference. Could you point me on how to get the expected behaviour? Happy to do changes and submit a pull request if appropriate.
Hi!
Just trying to generate pink noise using this package. I am not familiar with knowledge of color noise. However, according to Wikipedia, it seems that the pink noise will show obvious pattern when the x-axis
is frequency and the y-axis
is power.
Do you have such module that I can generate a numpy-array with this frequency-power format? Or any ways that I can check if the generated plots obey the distribution?
Thanks a lot!
Hi,
If I generate a brownian noise with 16000 sample points using your library (something like y = powerlaw_psd_gaussian(2.0, 16000)
), and save it with a sample rate of 16000, its fft spectrum looks as follows:
If I generate the same noise with 16000000 samples and save it using the same sample rate as before (16000), the fft spectrum looks like:
As you can see, the spectrum looks quite different, and they actually sound very different. Is there anything wrong with my way of using your code? Also, as you can see in the second figure, there is a drastic drop in the wave plot near the end of the wave. It seems the wave drop to a very low value and comes back immediately in the point after. Is this normal?
Thanks for the help!
Hi,
Thanks for this great little module. I am using it quite a bit in my academic work, and would love to cite it but there isn't any way to do so (at least not in a way that is useful / pushes up your h-index). Since its download statistics are pretty solid, I suspect that I am not the only one. May I suggest that you submit a brief write-up to a citeable institution? I have had good experiences with the Journal of Open Source Science with one of my own projects (otherwise not affiliated in any way).
All the best,
Paul
PS: I just saw on your Google Scholar profile that you used to work with Klaus Pawelzik. For two years, Klaus spent most of holidays teaching me and a bunch of other kids computational neuroscience. Small world.
This may be a lack of understanding of the underlying mechanics on my part, but I am getting back NaN and inf filled Arrays when setting fmin to any value > 0.5 when the exponent is positive. For white noise it doesn't cause NaN/inf, and a seemingly normal array is returned, by a FFT on the signal shows that frequencies below the minimum still appear to be present.
Some examples of calls that will replicate
powerlaw_psd_gaussian(2, 16000, fmin=100)
powerlaw_psd_gaussian(1, 16000, fmin=5)
powerlaw_psd_gaussian(0.1, 16000, fmin=1000)
Examples of calls that will not cause error, but don't appear to remove the minimum frequency.
powerlaw_psd_gaussian(0, 16000, fmin=100)
powerlaw_psd_gaussian(-1, 16000, fmin=5000)
Please let me know if any additional info is needed. Thank you!
Hello there,
First of all thanks for the amazing library. I have a usecase where I need 2D colored noise. I suspect that simply generating a long array and resize it into a 2D matrix is not what I want as the power spectra on the second axis may not be correct. Curious if you have any suggestion on this?
Thanks a lot,
Yu
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.