<jupyter_start><jupyter_text>Generating the data with Python<jupyter_code># Let's see if FFT works as intended:
# We generate data with a noisy sin function and see what frequencies there are.
# we should expect a certain dominant frequency

noise = 5
time_step = 0.2
period = 10

time_vec = np.arange(0, 20, time_step)
wave = (10 * np.sin(2 * np.pi / period * time_vec))
noisy_wave = wave + noise * np.random.randn(time_vec.size)
plt.figure(figsize=(25, 5))
plt.scatter(time_vec, noisy_wave)
plt.plot(time_vec, wave, 'c')

plt.show()
if (-np.amin(noisy_wave)>np.amax(noisy_wave)):
    norm = np.amin(noisy_wave)
else:
    norm = np.amax(noisy_wave)
# Now we have to save our raw data into a file named input.txt
filename = 'input.txt'<jupyter_output><empty_output><jupyter_text>Frequency of the data analysis<jupyter_code># The FFT of the signal
sig_fft = fftpack.fft(noisy_wave)

# And the power (sig_fft is of complex dtype)
power = np.abs(sig_fft)

# The corresponding frequencies
sample_freq = fftpack.fftfreq(noisy_wave.size, d=time_step)

# Plot the FFT power
plt.figure(figsize=(6, 5))
plt.xlim(0,1)
plt.plot(sample_freq, power)
plt.xlabel('Frequency [Hz]')
plt.ylabel('power')
# Now we have to save our array in a txt file
filename = 'input.txt'

np.savetxt(filename, noisy_wave, delimiter='\n',fmt="%i")<jupyter_output><empty_output><jupyter_text>Deriving the coefficients $C_i$<jupyter_code>from scipy import signal
numtaps = 4
f = 0.1

signal.firwin(numtaps, f)<jupyter_output><empty_output><jupyter_text>Filtering the data<jupyter_code>import sys
! ./build.sh<jupyter_output>Compiling support vhd files:
  1/2 flipflop.vhd
  2/2 flipflop_sum.vhd
Compiling main vhd file:
  1/1 fir_filter.vhd
Compiling testbench file:
  1/1 fir_filter_tb.vhd
[1mfir_filter_tb.vhd:44:19:[1;31merror:[0;1m actual constraints don't match formal ones[0m
[1mfir_filter_tb.vhd:35:3:[1;31merror:[0;1m port "nxt" of mode IN must be connected[0m
Running main entity:
  fir_filter_4
Running testbench:
 fir_filter_4_tb
[1mghdl:[1;31merror:[0;1m cannot find entity or configuration fir_filter_4_tb[0m
Done<jupyter_text>Analyzing the data after FIR filtering<jupyter_code>filtered = np.loadtxt("output.txt", dtype=np.int)
filtered
plt.figure(figsize=(25, 5))
plt.scatter(time_vec, filtered)
fmin = -np.amin(filtered)
fmax = np.amax(filtered)
if (fmin > fmax):
    fnorm = fmin
else:
    fnorm = fmax
filtered = norm * filtered/fnorm
plt.figure(figsize=(25, 5))
plt.scatter(time_vec, -filtered)
# The FFT of the signal
f_fft = fftpack.fft(filtered)

# And the power (sig_fft is of complex dtype)
power = np.abs(f_fft)

# The corresponding frequencies
f_freq = fftpack.fftfreq(filtered.size, d=time_step)

# Plot the FFT power
plt.figure(figsize=(6, 5))
plt.xlim(0,2)
plt.plot(f_freq, power)
plt.xlabel('Frequency [Hz]')
plt.ylabel('power')
plt.figure(figsize=(25, 5))
plt.scatter(time_vec, filtered, alpha=0.5)
plt.scatter(time_vec+0.2, noisy_wave, alpha =0.5,color='r')
plt.plot(time_vec, wave, 'k')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13024793388429753, 'fraction_numerical': 0.026115702479338844, 'mean_word_length': 5.908675799086758, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '579163', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 1274, 'n_tokens_neox': 1193, 'n_words': 282, 'path': 'testbench/.ipynb_checkpoints/generator-checkpoint.ipynb', 'repo_name': 'SaverioMonaco/4--Taps-FIR-Filter', 'size': '3025'}