1#!/usr/bin/python
2
3# ----------------------------------------------------------------------------
4#
5#  Copyright (C) 2012-2020 Fons Adriaensen <fons@linuxaudio.org>
6#
7#  This program is free software; you can redistribute it and/or modify
8#  it under the terms of the GNU General Public License as published by
9#  the Free Software Foundation; either version 3 of the License, or
10#  (at your option) any later version.
11#
12#  This program is distributed in the hope that it will be useful,
13#  but WITHOUT ANY WARRANTY; without even the implied warranty of
14#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15#  GNU General Public License for more details.
16#
17#  You should have received a copy of the GNU General Public License
18#  along with this program.  If not, see <http:#www.gnu.org/licenses/>.
19#
20# ----------------------------------------------------------------------------
21
22
23import numpy as np
24import matplotlib.pyplot as plt
25from jacktools.jackiecfilt import JackIECfilt
26from jacktools.jacksignal import JackSignal
27
28
29# Measure frequency response of third octave band filters in JackIECfilt.
30#
31
32# We measure four filters at a time.
33# Create and connect objects.
34#
35J = JackSignal("JackSignal")
36if J.get_state() < 0:
37    print ("Failed to create JackSignal -- is Jack running ?")
38    exit(1)
39J.create_output (0, "out")
40J.create_input (0, "in")
41J.silence()
42
43F = JackIECfilt (1, 4, "JackIECfilt")
44F.connect_input (0, "JackSignal:out")
45for i in range (4):
46    F.connect_output (i, "JackSignal:in")
47
48# Get Jack info.
49#
50name, fsamp, period = J.get_jack_info ()
51
52# Parameters
53#
54fftlen = 64 * 1024
55
56# Generate data.
57#
58Aout = np.zeros ([10,], dtype = np.float32)
59Aout [0] = 1.0
60Ain1 = np.empty ([fftlen,], dtype = np.float32)
61Freq = np.linspace (0, fsamp / 2, num = fftlen // 2 + 1)
62
63J.set_output_data (0, Aout)
64J.set_input_data (0, Ain1, nskip = period)
65
66# Set up plot.
67#
68fig = plt.figure (figsize=(8,5), facecolor='white')
69fig.canvas.set_window_title ('oct1band')
70ax1 = fig.add_axes ([0.06, 0.06, 0.90, 0.90])
71ax1.set_xlim (1e1, 24e3)
72ax1.set_ylim (-50, 5)
73ax1.set_xscale ('log')
74ax1.grid ()
75
76
77# Measure and plot all bands, four at a time.
78#
79for i in range (0, 8):
80    print ("measuring bands", end = "")
81    for j in range (0, 4):
82        k = i + 8 * j
83        if k <= 30:
84            F.set_filter (0, j, 3, k)
85            print (" %2d" % (k,), end = "")
86        else:
87            F.set_filter (0, j, 0, 0)
88    print ()
89    J.process()
90    J.wait()
91    Spec = np.fft.rfft (Ain1)
92    ax1.plot (Freq, 20 * np.log10 (np.abs (Spec) + 1e-10), color='b', lw=1)
93
94# Show result.
95#
96del J, F
97plt.show()
98