1#!/usr/bin/python
2
3# ----------------------------------------------------------------------------
4#
5#  Copyright (C) 2012-2018 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 math import *
26from time import sleep
27from jacktools.jackiecfilt import JackIECfilt
28from jacktools.jacksignal import JackSignal
29
30
31# Measure frequency response of octave band filters in JackIECfilt.
32#
33
34# Create and connect objects.
35#
36J = JackSignal("JackSignal")
37if J.get_state() < 0:
38    print ("Failed to create JackSignal -- is Jack running ?")
39    exit(1)
40J.create_output (0, "out")
41J.create_input (0, "in")
42J.silence()
43
44F = JackIECfilt (1, 1, "JackIECfilt")
45
46J.connect_output (0, "JackIECfilt:in_0")
47J.connect_input (0, "JackIECfilt:out_0")
48
49# Get Jack info.
50#
51name, fsamp, period = J.get_jack_info ()
52
53# Parameters.
54#
55impval = 1.0
56fftlen = 64 * 1024
57
58# Generate data.
59#
60Aout = np.zeros ([100,], dtype = np.float32)
61Aout [0] = impval
62
63Ain1 = np.empty ([fftlen,], dtype = np.float32)
64Freq = np.linspace (0, fsamp / 2, num = fftlen // 2 + 1)
65
66J.set_output_data (0, Aout)
67J.set_input_data (0, Ain1, nskip = period)
68
69# Set up plot.
70#
71fig = plt.figure (figsize=(8,5), facecolor='white')
72fig.canvas.set_window_title ('oct1band')
73ax1 = fig.add_axes ([0.06, 0.06, 0.90, 0.90])
74ax1.set_xlim (1e1, 24e3)
75ax1.set_ylim (-50, 5)
76ax1.set_xscale ('log')
77ax1.grid ()
78
79# Measure and plot each band.
80#
81for i in range (10):
82    print ("measuring band", i)
83    F.set_filter (0, 0, 1, i)
84    J.process()
85    J.wait()
86    Spec = np.fft.rfft (Ain1)
87    ax1.plot (Freq, 20 * np.log10 (np.abs (Spec) + 1e-10), color='b', lw=1)
88
89# Show result.
90#
91del J, F
92plt.show()
93