aboutsummaryrefslogtreecommitdiffstats
path: root/src/atrac/atrac3plus_pqf/tools/plot/main.py
blob: 2296196227ba3bd5d6a0b74cd55c52fac8ded468 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import argparse

import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack

import pqf

SAMPLES = pqf.frame_sz
RUNS = 3
SAMPLERATE = 44100

def show_proto():
    proto = pqf.prototype()
    fig = plt.figure()

    yf = scipy.fftpack.fft(proto[:len(proto)])

    fig.add_subplot(111).plot(10 * np.log10(np.abs(yf[:len(proto)//2])))
    fig.add_subplot(222).plot(proto)
    plt.show()


def do_an(ctx, time, data, shift):
    data_slice = data[shift : SAMPLES + shift]
    time_slice = time[shift : SAMPLES + shift]

    res = ctx.do(data_slice);

    fig = plt.figure()

    a = fig.add_subplot(111)
    a.plot(time_slice, res)
    a.set_title("output")
    b = fig.add_subplot(222)
    b.plot(time_slice, data_slice)
    b.set_title("input");
    plt.show()


def process_freq(freq):
    ctx = pqf.AnalyzeCtx()

    time = np.arange(0, SAMPLES * RUNS, 1)
    amplitude = 0.01 * np.sin(2 * np.pi * freq * time / SAMPLERATE).astype(np.float32)

    do_an(ctx, time, amplitude, 0);
    do_an(ctx, time, amplitude, SAMPLES);


def main():
    parser = argparse.ArgumentParser()

    parser.add_argument('--freq', type=int, help="generate sine with given frequency", action='store')
    parser.add_argument('--proto', help="show filter prototype", action='store_true')

    args = parser.parse_args()

    if (args.proto):
        show_proto()
    else:
        process_freq(args.freq)


if __name__ == "__main__":
    main()