LASP 1.0
Library for Acoustic Signal Processing
Loading...
Searching...
No Matches
colorednoise.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3"""!
4Author: J.A. de Jong - ASCEE
5
6Description: Implementation of SOS filters to color a signal. Typically used to
7color white noise to i.e. Pink noise, or Brownian noise.
8"""
9from scipy.signal import bilinear_zpk, zpk2sos, freqz_zpk
10import numpy as np
11
12__all__ = ['PinkNoise'] #, 'BrownianNoise', 'BlueNoise']
13
14
15def PinkNoise(fs, fstart=10, fend=None, N=3):
16 """
17 Creates SOS filter for pink noise. The filter has a flat response below
18 fstart, and rolls of close to Nyquist, or flattens out above fend. The
19 ripple (i.e.) closeness the filter rolls of depends on the number of
20 sections. The default, N=3 results in
21
22 Args:
23 fs: Sampling frequency [Hz]
24 fstart: Frequency of first pole of the filter
25 fend: Frequency of last pole of the filter, if not given, set to 5/6th
26 of the Nyquist frequency.
27 N: Number of sections.
28
29 Returns:
30 sos: Array of digital filter coefficients
31
32 """
33 order = N*2
34 if fend is None:
35 fend = 5*fs/6/2
36 # Not the fastest implementation, but this will not be the bottleneck
37 # anyhow.
38 fpoles = np.array([fstart*(fend/fstart)**(n/(order-1))
39 for n in range(order)])
40
41 # Put zeros inbetween poles
42 fzeros = np.sqrt(fpoles[1:]*fpoles[:-1])
43
44 poles = -2*np.pi*fpoles
45 zeros = -2*np.pi*fzeros
46
47 z,p,k = bilinear_zpk(zeros, poles,1, fs=fs)
48
49 # Compute the frequency response and search for the gain at fstart, we
50 # normalize such that this gain is ~ 0 dB. Rounded to an integer frequency
51 # in Hz.
52 Omg, h = freqz_zpk(z, p, k, worN = int(fs/2), fs=fs)
53 h_fstart = np.abs(h[int(fstart)])
54 k *= 1/h_fstart
55
56 sos = zpk2sos(z,p,k)
57
58 return sos
PinkNoise(fs, fstart=10, fend=None, N=3)
Creates SOS filter for pink noise.