aboutsummaryrefslogtreecommitdiffstats
path: root/src/gen_source.py
blob: 7620bc32d141d1260a3c4599d59c7873417d9ee0 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import numpy as np


def complex_exp(freq, samp_rate, periods, phase_deg=0):
    t_max = 1.0 * samp_rate / freq * periods
    t = np.arange(t_max)
    fac = t / samp_rate * freq
    phase = 1j * phase_deg/360*2*np.pi
    ret = np.exp(phase + 1j * 2 * np.pi * fac - 1j * np.pi / 2, dtype=np.complex128)
    return ret
ret = complex_exp(10,40,2)


def gen_file(frequency_0, frequency_1, coefs = [], samp_rate = 4000000, path = "./np_twotone", count = 1):
    tone_0 = complex_exp(frequency_0, samp_rate, samp_rate/frequency_1 * count)
    tone_1 = complex_exp(frequency_1, samp_rate, samp_rate/frequency_0 * count)

    min_len = min(tone_0.shape[0], tone_1.shape[0])
    two_tone = (tone_0[0:min_len] + tone_1[0:min_len])
    res = two_tone

    for idx, coef in enumerate(coefs):
        res += two_tone * np.abs(two_tone)**(idx+1) * coef #+1 because first correction term is squared

    res = res / np.max(res) * 0.9

    res = res.astype(np.complex64)
    res.tofile(path)

    a_load = np.fromfile(path, dtype=np.complex64)
    assert(np.isclose(a_load, res).all()), "Inconsistent stored file"

    return path

def gen_file_d(frequency_0, frequency_1, x1 = 0, x2 = 0, x3 = 0, x4 = 0, samp_rate = 4000000, path = "./np_twotone", count = 1):
    tone_0 = complex_exp(frequency_0, samp_rate, samp_rate/frequency_1 * count)
    tone_1 = complex_exp(frequency_1, samp_rate, samp_rate/frequency_0 * count)

    min_len = min(tone_0.shape[0], tone_1.shape[0])
    two_tone = (tone_0[0:min_len] + tone_1[0:min_len])

    two_tone_1 = np.gradient(two_tone)
    two_tone_2 = np.gradient(two_tone_1)
    two_tone_3 = np.gradient(two_tone_2)
    two_tone_4 = np.gradient(two_tone_3)

    two_tone = two_tone \
            + two_tone_1 * x1 \
            + two_tone_2 * x2 \
            + two_tone_3 * x3 \
            + two_tone_4 * x4

    two_tone = two_tone / np.max(two_tone) * 0.9

    two_tone = two_tone.astype(np.complex64)
    two_tone.tofile(path)

    a_load = np.fromfile(path, dtype=np.complex64)
    assert(np.isclose(a_load, two_tone).all()), "Inconsistent stored file"

    return path

def gen_file_i(frequency_0, frequency_1, x1 = 0, x2 = 0, x3 = 0, x4 = 0, samp_rate = 4000000, path = "./np_twotone", count = 1):
    if frequency_0 > frequency_1:
        f = frequency_0
        frequency_1 = frequency_0
        frequency_0 = f

    tone_0 = complex_exp(frequency_0, samp_rate, samp_rate/frequency_1 * count)
    tone_1 = complex_exp(frequency_1, samp_rate, samp_rate/frequency_0 * count)

    df = frequency_1 - frequency_0
    tone_0_0 = complex_exp(frequency_0 - df, samp_rate, samp_rate/frequency_1 * count, pahse = x2)
    tone_0_1 = complex_exp(frequency_1 + df, samp_rate, samp_rate/frequency_1 * count, pahse = x2)


    min_len = min(tone_0.shape[0], tone_1.shape[0])
    two_tone = (tone_0[0:min_len] + tone_1[0:min_len])

    two_tone_1 = np.gradient(two_tone)
    two_tone_2 = np.gradient(two_tone_1)
    two_tone_3 = np.gradient(two_tone_2)
    two_tone_4 = np.gradient(two_tone_3)

    two_tone = two_tone \
            + two_tone_1 * x1 \
            + two_tone_2 * x2 \
            + two_tone_3 * x3 \
            + two_tone_4 * x4

    two_tone = two_tone / np.max(two_tone) * 0.9

    two_tone = two_tone.astype(np.complex64)
    two_tone.tofile(path)

    a_load = np.fromfile(path, dtype=np.complex64)
    assert(np.isclose(a_load, two_tone).all()), "Inconsistent stored file"

    return path