[ci] add pip install to readthedocs.yaml
[aubio.git] / python / demos / demo_filterbank.py
1 #! /usr/bin/env python
2
3 """Create a filterbank from a list of frequencies.
4
5 This demo uses `aubio.filterbank.set_triangle_bands` to build a set of
6 triangular filters from a list of frequencies.
7
8 The filterbank coefficients are then modified before being displayed."""
9
10 import aubio
11 import numpy as np
12 import matplotlib.pyplot as plt
13
14 # sampling rate and size of the fft
15 samplerate = 48000
16 win_s = 2048
17
18 # define a list of custom frequency
19 freq_list = [60, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 24000]
20 # number of filters to create
21 n_filters = len(freq_list) - 2
22
23 # create a new filterbank
24 f = aubio.filterbank(n_filters, win_s)
25 freqs = aubio.fvec(freq_list)
26 f.set_triangle_bands(freqs, samplerate)
27
28 # get the coefficients from the filterbank
29 coeffs = f.get_coeffs()
30 # apply a gain to fifth band
31 coeffs[4] *= 6.
32 # load the modified coeffs into the filterbank
33 f.set_coeffs(coeffs)
34
35 # display the band gains in a loglog plot
36 freqs = np.vstack([np.arange(win_s // 2 + 1) * samplerate / win_s] * n_filters)
37 plt.title('filterbank built from a list of frequencies\n'
38           'The 5th band has been amplified by a factor 6.')
39 plt.loglog(freqs.T, f.get_coeffs().T, '.-')
40 plt.xlim([50, samplerate/2])
41 plt.ylim([1.0e-6, 2.0e-2])
42 plt.xlabel('log frequency (Hz)')
43 plt.ylabel('log amplitude')
44 plt.show()