0
1
mirror of https://github.com/radio95-rnt/rds95.git synced 2026-02-26 20:33:53 +01:00
Files
rds95/gen_wave.py
2025-05-01 14:05:14 +02:00

70 lines
1.6 KiB
Python

PLOT = False
FFT = PLOT and False
import math
import io, os
if PLOT: import matplotlib.pyplot as plt
if FFT: import numpy as np
DATA_RATE = 1187.5
ratio = 4
sample_rate = DATA_RATE*ratio
print(f"{sample_rate=}")
if not sample_rate.is_integer(): raise ValueError("Need a even value")
PATH = os.path.dirname(os.path.abspath(__file__))
outc = io.open(f"{PATH}/src/waveforms.c", mode="w", encoding="utf8")
outh = io.open(f"{PATH}/src/waveforms.h", mode="w", encoding="utf8")
header = u"""
/* This file was automatically generated by "gen_wave.py".
(C) 2014 Christophe Jacquet.
(C) 2023 Anthony96922.
(C) 2025 kuba201.
Released under the GNU GPL v3 license.
*/
"""
outc.write(header)
outh.write(header)
def generate():
t = [i / sample_rate for i in range(ratio)]
out = [math.sin(2 * math.pi * DATA_RATE * time) for time in t]
print(f"{len(out)=}")
if PLOT:
plt.plot(out*4, label="out")
plt.legend()
plt.axvline(x=len(out)*2, color='r', linestyle='--', label='center')
plt.grid(True)
plt.show()
if FFT:
N = len(out)
fft_out = np.fft.fft(out)
fft_freqs = np.fft.fftfreq(N, d=1/sample_rate)
plt.figure(figsize=(10, 6))
plt.plot(fft_freqs[:N//2], np.abs(fft_out)[:N//2])
plt.xlim(0,DATA_RATE*2.5)
plt.title("FFT of the waveform")
plt.xlabel("Frequency (Hz)")
plt.ylabel("Magnitude")
plt.grid(True)
plt.show()
outc.write(u"float waveform_biphase[{size}] = {{{values}}};\n\n".format(
values = u", ".join(map(str, out)),
size = len(out)))
outh.write(u"extern float waveform_biphase[{size}];\n".format(size=len(out)))
generate()
outc.close()
outh.close()