|
| 1 | +import numpy as np |
| 2 | +import matplotlib |
| 3 | +matplotlib.use('Agg') # Non-interactive backend for saving files |
| 4 | +import matplotlib.pyplot as plt |
| 5 | + |
| 6 | +####################################################### |
| 7 | +# WARNING: !!!WIP!!! |
| 8 | +####################################################### |
| 9 | +def generate_sine_wave(freq, sample_rate, n_samples): |
| 10 | + t = np.arange(n_samples) / sample_rate |
| 11 | + sine_wave = np.sin(2 * np.pi * freq * t) |
| 12 | + return t, sine_wave |
| 13 | + |
| 14 | +def save_input_data(time, signal, filename="input_signal.txt"): |
| 15 | + with open(filename, 'w') as f: |
| 16 | + f.write("Time(seconds),Amplitude\n") |
| 17 | + for t, s in zip(time, signal): |
| 18 | + f.write(f"{t:.9f},{s:.9f}\n") |
| 19 | + print(f"Input signal data saved as {filename}") |
| 20 | + |
| 21 | +def plot_signal(time, signal, filename="input_signal.png"): |
| 22 | + plt.figure(figsize=(10, 4)) |
| 23 | + plt.plot(time, signal, marker='o', linestyle='-', markersize=6, color='b') |
| 24 | + plt.title("Input Sine Wave Samples") |
| 25 | + plt.xlabel("Time (seconds)") |
| 26 | + plt.ylabel("Amplitude") |
| 27 | + plt.grid(True) |
| 28 | + plt.savefig(filename) |
| 29 | + plt.close() |
| 30 | + print(f"Input signal plot saved as {filename}") |
| 31 | + |
| 32 | +def plot_and_save_fft(signal, sample_rate, fft_size=64, |
| 33 | + plot_filename="fft_output.png", |
| 34 | + data_filename="fft_output.txt"): |
| 35 | + n = fft_size |
| 36 | + fft_vals = np.fft.fft(signal, n=n) |
| 37 | + fft_freqs = np.fft.fftfreq(n, 1/sample_rate) |
| 38 | + |
| 39 | + magnitude = np.abs(fft_vals) * 2 / n |
| 40 | + phase = np.angle(fft_vals) |
| 41 | + |
| 42 | + # Plot single-sided magnitude spectrum |
| 43 | + pos_mask = fft_freqs >= 0 |
| 44 | + freqs_pos = fft_freqs[pos_mask] |
| 45 | + mag_pos = magnitude[pos_mask] |
| 46 | + |
| 47 | + plt.figure(figsize=(10, 6)) |
| 48 | + plt.plot(freqs_pos, mag_pos, marker='o') |
| 49 | + plt.title('FFT Magnitude Spectrum (Single-Sided)') |
| 50 | + plt.xlabel('Frequency (Hz)') |
| 51 | + plt.ylabel('Magnitude') |
| 52 | + plt.grid(True) |
| 53 | + plt.savefig(plot_filename) |
| 54 | + plt.close() |
| 55 | + print(f"FFT magnitude plot saved as {plot_filename}") |
| 56 | + |
| 57 | + # Save all FFT data points to text file |
| 58 | + with open(data_filename, 'w') as f: |
| 59 | + f.write("Frequency(Hz),Magnitude,Phase(radians)\n") |
| 60 | + for freq, mag, ph in zip(fft_freqs, magnitude, phase): |
| 61 | + f.write(f"{freq:.6f},{mag:.6f},{ph:.6f}\n") |
| 62 | + print(f"Full FFT data saved as {data_filename}") |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + fs = 1024 # Sample rate in Hz |
| 66 | + f = 60 # Sine wave frequency in Hz |
| 67 | + N = 64 # Number of samples / FFT size |
| 68 | + |
| 69 | + time, sine = generate_sine_wave(f, fs, N) |
| 70 | + save_input_data(time, sine, filename="input_signal.txt") |
| 71 | + plot_signal(time, sine, filename="input_signal.png") |
| 72 | + plot_and_save_fft(sine, fs, fft_size=N, |
| 73 | + plot_filename="fft_output.png", |
| 74 | + data_filename="fft_output.txt") |
0 commit comments