1#!/usr/bin/python
2
3# ----------------------------------------------------------------------------
4#
5#  Copyright (C) 2008-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 math import *
26from time import sleep
27from jacktools.jacksignal import JackSignal
28
29
30# ------- Frequency response measurement using impulse and FFT  ---------
31#
32# Make sure jnoisemeter is running, using
33# input 1, and select the A, C or ITU filter.
34
35# Create a JackSignal object and connect.
36#
37J = JackSignal("JackSignal")
38if J.get_state() < 0:
39    print ("Failed to create JackSignal -- is Jack running ?")
40    exit(1)
41J.create_output (0, "out-1")
42J.create_input (0, "in-1")
43
44J.silence()
45J.connect_output (0, "jnoisemeter:in_1")
46J.connect_input (0, "jnoisemeter:out")
47
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) # Skip one period.
68
69# Run the test
70#
71J.process()
72J.wait ()
73del J
74
75# Process the result
76#
77Spec = np.fft.rfft (Ain1)
78
79# Display impulse and magnitude response.
80#
81fig = plt.figure (figsize=(8,6), facecolor='white')
82ax1 = fig.add_axes ([0.07, 0.04, 0.90, 0.44])
83ax1.set_ylim (-1.5, 1.5)
84ax1.plot (Ain1 [0:100], color='b', lw=1)
85ax1.grid ()
86ax2 = fig.add_axes ([0.07, 0.53, 0.90, 0.44])
87ax2.set_xlim (20, 20e3)
88ax2.set_ylim (-60, 15)
89ax2.set_xscale ('log')
90ax2.plot (Freq, 20 * np.log10 (np.abs (Spec) + 1e-10), color='b', lw=1)
91ax2.grid ()
92plt.show()
93