# for 5 seconds
# with a total signal rms of 1 volt
time = nm.arange(0.0,5.0,0.01) # seconds
# let's do this by actually having ten sine waves of equal amplitude superposed
# the amplitude of each will need to be 0.1*sqrt(2) to get the total amplitude to be sqrt(2)
# the rms of that will be a factor of sqrt(2) smaller, i.e. 1.0
f_signals = nm.arange(2.950,3.005,0.005) # i.e. 2.95 to 3.0
voltage = nm.zeros_like(time)
for f in f_signals:
	voltage = voltage + 0.1*nm.sqrt(2.0)*nm.cos(2.0*nm.pi*f*time)

# now add white noise whose rms across all frequencies is 0.25
voltage = voltage + 0.25*nm.random.randn(len(voltage))

# take the power spectrum
freq,v_sqrt_hz = amplitude_and_power_spectrum(voltage, 0.01, return_amplitudes=False)

# do the rms integral from 2.6 to 3.6 Hz to measure the rms signal power
df = nm.mean(nm.diff(freq))
# note that freq[13] = 2.6 Hz and freq[18] = 3.6 Hz
rms_signal = nm.sqrt(nm.sum(v_sqrt_hz[13:19]**2.0)*df)

# do the rms integral at all frequencies that are NOT in the range 2.6 to 3.6 Hz to measure the rms noise power
rms_noise = nm.sqrt(nm.sum(v_sqrt_hz[0:13]**2.0)*df + nm.sum(v_sqrt_hz[19:]**2.0)*df)

# plot everything
import pylab
pylab.figure(1)
pylab.clf()
pylab.subplot(211)
pylab.plot(time,voltage)
from amplitude_and_power_spectrum import amplitude_and_power_spectrum

# generate a sine wave at 3 Hz
# sampled 100 times per second
# for 60 seconds
# with a phase angle of 20 degrees
# an amplitude of 1 volt
# and a DC offset of 2 volts
time = nm.arange(0.0,60.0,0.01) # seconds
f_signal = 3.0 # Hz
phase_angle = 20.0 # degrees
# note that exp(-ix) = cos(x) - i*sin(x)
voltage = nm.cos(2.0*nm.pi*f_signal*time)*nm.cos(phase_angle*(nm.pi/180.0)) - nm.sin(2.0*nm.pi*f_signal*time)*nm.sin(phase_angle*(nm.pi/180.0)) + 2.0

# take the amplitude spectrum
freq,amplitudes,phase = amplitude_and_power_spectrum(voltage, 0.01, return_amplitudes=True)

# plot everything
import pylab
pylab.figure(1)
pylab.clf()
pylab.subplot(211)
pylab.plot(time,voltage)
pylab.xlabel('Time [s]')
pylab.ylabel('Voltage [V]')
pylab.title('Voltage Signal: 3 Hz Sine Wave for 60 Seconds')
pylab.subplot(212)
pylab.plot(time,voltage,linestyle='None',marker='*')
pylab.xlabel('Time [s]')
pylab.ylabel('Voltage [V]')
pylab.xlim([0,1.0])