<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Analyzing spectrum of mains frequency with Arduino UNO
To use the analog digital converter from Arduino UNO it is necessary to decrease the 230 V~ to an appropiate range
Step1:  Imports
Step2:  Parameters
Step3:  Read Data from A/D Converter
Step4:  Plot Time Domain
Step5:  Plot Frequency Domain
Step6:  linear and around 50 Hz
Step7:  linear with harmonics
Step8:  dB with harmonics
<ASSISTANT_TASK:>
Python Code:
from IPython.display import Image
Image(filename='circuit.png') 
Explanation: Analyzing spectrum of mains frequency with Arduino UNO
To use the analog digital converter from Arduino UNO it is necessary to decrease the 230 V~ to an appropiate range:
"Reads the value from the specified analog pin. The Arduino board contains a 6 channel (8 channels on the Mini and Nano, 16 on the Mega), 10-bit analog to digital converter. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution between readings of: 5 volts / 1024 units or, .0049 volts (4.9 mV) per unit. " (source: https://www.arduino.cc/en/Reference/AnalogRead)
Therefore, a power adapter transforms 230 V~ to 17 V~ and a voltage divider and an additional 5 V DC supply convert the voltage in a range between 0...5 V (see picture below).
End of explanation
import numpy as np
import matplotlib.pyplot as plt
import plotting
%matplotlib inline
Explanation: Imports
End of explanation
fs = 2428.35  # sampling frequency 
bit = 10  # arduino resolution
resolution = 5 / 2**10  # arduino range: 0...5 Volt
dc_offset = 2.45  # Volts
Explanation: Parameters
End of explanation
# 1) convert ADC value to a voltage level
# 2) remove DC offset
# 3) convert to 230 V level
data = (np.genfromtxt('analog_data.txt') * resolution - dc_offset) * 114
Explanation: Read Data from A/D Converter
End of explanation
plotting.plot_time(data, fs)
plt.ylim([-250,250])
plt.xlim([0,0.5])
plt.ylabel("U / V")
plt.title("AC Voltage");
Explanation: Plot Time Domain
End of explanation
maximum = np.max(data)
Explanation: Plot Frequency Domain
End of explanation
plotting.plot_freq(data[:100000] / maximum, fs, stem=True)
plt.xlim([49.8, 50.2])
plt.title("Frequency Spectrum around 50 Hz");
Explanation: linear and around 50 Hz
End of explanation
plotting.plot_freq(data / maximum, fs)
plt.ylim([0, 0.04])
plt.xlim([1, 400])
plt.title("Harmonics in Mains Frequency");
Explanation: linear with harmonics
End of explanation
plotting.plot_freq(data, fs, scale='db')
plt.xlim([1, fs/2])
plt.ylim([-55, 50])
plt.title("Harmonics in Mains Frequency (logarithmic)");
Explanation: dB with harmonics
End of explanation
<END_TASK>