Skip to content

Commit

Permalink
Update examples/spectrum_analyzer.py
Browse files Browse the repository at this point in the history
  • Loading branch information
minjungkh committed May 24, 2017
1 parent 1674a88 commit 0e27235
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions examples/spectrum_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,63 @@

from koheron import connect
from ldk.drivers import Spectrum
from ldk.drivers import Laser

# Load the spectrum instrument
host = os.getenv('HOST','192.168.1.100')
client = connect(host, name='spectrum')
driver = Spectrum(client)
laser = Laser(client)

# Enable laser
driver.start_laser()
laser.start_laser()

# Set laser current
current = 30 # mA
driver.set_laser_current(current)
current = 30 # mA
laser.set_laser_current(current)

# driver.reset_acquisition()

wfm_size = 4096
decimation_factor = 1
index_low = 0
index_high = wfm_size / 2

signal = driver.get_decimated_data(decimation_factor, index_low, index_high)
print('Signal')
print(signal)

mhz = 1e6
sampling_rate = 125e6
freq_min = 0
freq_max = sampling_rate / mhz / 2

# Plot parameters
fig = plt.figure()
ax = fig.add_subplot(111)
x = 1e-6 * np.fft.fftshift(driver.sampling.f_fft)
y = 10*np.log10(np.fft.fftshift(driver.spectrum))

x = np.linspace(freq_min, freq_max, (wfm_size / 2))
print('X')
print(len(x))

y = 10*np.log10(signal)

print('Y')
print(len(y))

li, = ax.plot(x, y)
fig.canvas.draw()
ax.set_xlim((x[0],x[-1]))
ax.set_ylim((100,250))
ax.set_ylim((0,200))
ax.set_xlabel('Frequency (MHz)')
ax.set_ylabel('Power spectral density (dB)')

while True:
try:
driver.get_spectrum()
li.set_ydata(10*np.log10(np.fft.fftshift(driver.spectrum)))
signal = driver.get_decimated_data(decimation_factor, index_low, index_high)
li.set_ydata(10*np.log10(signal))
fig.canvas.draw()
plt.pause(0.001)
except KeyboardInterrupt:
# Save last spectrum in a csv file
np.savetxt("psd.csv", driver.spectrum, delimiter=",")
driver.stop_laser()
np.savetxt("psd.csv", signal, delimiter=",")
laser.stop_laser()
driver.close()
break

0 comments on commit 0e27235

Please sign in to comment.