mirror of
https://github.com/radio95-rnt/rds95.git
synced 2026-02-27 04:43:52 +01:00
add fft to the genwave
This commit is contained in:
36
gen_wave.py
36
gen_wave.py
@@ -1,6 +1,10 @@
|
|||||||
|
PLOT = True
|
||||||
|
FFT = PLOT and True
|
||||||
|
|
||||||
import math
|
import math
|
||||||
import io, os
|
import io, os
|
||||||
import matplotlib.pyplot as plt
|
if PLOT: import matplotlib.pyplot as plt
|
||||||
|
if FFT: import numpy as np # Import numpy for FFT
|
||||||
|
|
||||||
sample_rate = 9500
|
sample_rate = 9500
|
||||||
|
|
||||||
@@ -72,22 +76,34 @@ def generate():
|
|||||||
out = [i/(max(sf)+0.1) for i in out]
|
out = [i/(max(sf)+0.1) for i in out]
|
||||||
if max(out) > 1 or min(out) < -1: print("clipped")
|
if max(out) > 1 or min(out) < -1: print("clipped")
|
||||||
|
|
||||||
# plt.plot(sf, label="sf")
|
if PLOT:
|
||||||
# plt.plot(shapedSamples, label="shapedSamples")
|
# Plot the waveform
|
||||||
plt.plot(out, label="out")
|
plt.plot(out, label="out")
|
||||||
plt.legend()
|
plt.legend()
|
||||||
plt.grid(True)
|
plt.grid(True)
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
|
if FFT:
|
||||||
|
# Compute the FFT of the waveform
|
||||||
|
N = len(out)
|
||||||
|
fft_out = np.fft.fft(out)
|
||||||
|
fft_freqs = np.fft.fftfreq(N, d=1/sample_rate)
|
||||||
|
|
||||||
|
# Plot the magnitude of the FFT
|
||||||
|
plt.figure(figsize=(10, 6))
|
||||||
|
plt.plot(fft_freqs[:N//2], np.abs(fft_out)[:N//2]) # Plot only the positive frequencies
|
||||||
|
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(
|
outc.write(u"float waveform_biphase[{size}] = {{{values}}};\n\n".format(
|
||||||
values = u", ".join(map(str, out)),
|
values = u", ".join(map(str, out)),
|
||||||
size = len(out)))
|
size = len(out)))
|
||||||
# note: need to limit the amplitude so as not to saturate when the biphase
|
|
||||||
# waveforms are summed
|
|
||||||
|
|
||||||
outh.write(u"extern float waveform_biphase[{size}];\n".format(size=len(out)))
|
outh.write(u"extern float waveform_biphase[{size}];\n".format(size=len(out)))
|
||||||
|
|
||||||
|
|
||||||
generate()
|
generate()
|
||||||
|
|
||||||
outc.close()
|
outc.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user