And here is my Python Code: import matplotlib.pyplot as plt from scipy.fftpack import fft from scipy.io import wavfile # get the api import serial, time import numpy as np import sounddevice as sd # %%
import matplotlib.pyplot as plt
from scipy.fftpack import fft
from scipy.io import wavfile # get the api
import serial, time
import numpy as np
import sounddevice as sd
arduino = serial.Serial('COM4', 9600, timeout=1) #COM port depends on the USB port
time.sleep(3) #time to get connection ready
# %%
fs, data = wavfile.read('hfsound.wav')
samples = len(data)
#Plot the signal
plt.plot(data[:samples])
plt.ylabel("Amplitude")
plt.xlabel("Time")
plt.title("Time Domain Response of the Input Signal")
plt.grid()
plt.show()
#Plotting fft
fft_input = fft(data)
half_length = int(len(fft_input)/2)
omega_axis = np.linspace(0, fs/2, half_length)
fft_n = fft_input[:half_length]
plt.plot(omega_axis,abs(fft_n))
plt.xlabel('frequency')
plt.ylabel('Amplitude')
plt.title("Frequency Domain Response of the Input Signal")
plt.grid()
plt.show()
# %%
#Declare an Array of zeros
outputArray = np.zeros(64000)
#Sending data to aurdino
for i in range(samples):
number = data[i]
string = str(number).encode(encoding='utf-8') + b'\n'
arduino.write(string)
byt = arduino.read_until();
print(byt.strip())
outputArray[i] = byt.strip()
arduino.close()
# %%
#Plotting Output waveform
#Plot the time-domain output signal
plt.plot(outputArray[:len(outputArray)])
plt.ylabel("Amplitude")
plt.xlabel("Time")
plt.title("Time Domain Response of the Output Signal")
plt.grid()
plt.show()
#Plotting fft of output
fft_out = fft(outputArray)
half_length = int(len(fft_out)/2)
omega_axis = np.linspace(0, fs/2, half_length)
fft_n = fft_out[:half_length]
plt.plot(omega_axis,abs(fft_n))
plt.xlabel('frequency')
plt.ylabel('Amplitude')
plt.title("Frequency Domain Response of the Output Signal")
plt.grid()
plt.show()
# %%
#Playback code
sd.play(outputArray, fs)
# %%