#!/usr/bin/env python # -*- coding: utf-8 -*- """ Control a CW Doppler radar based on the Innosent IVS-162. Daniel Sjoberg, 2018-02-04 """ # Import libraries from __future__ import print_function from pylab import * # Loads many 'matlab-like' commands from ADCDACPi import ADCDACPi import RPi.GPIO as GPIO # Settings for using the ADCDACPi GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) adcdac = ADCDACPi(1) # Radar and signal processing parameters adcdac.set_dac_voltage(2, 0.0) # Enable the radar module adcdac.set_dac_voltage(1, 0.0) # Set tuning voltage to zero N = 128 # Number of samples a = zeros(N, dtype=complex) # Storage for the analytic signal PlotMode = 'LivePlot' # Choose between 'LivePlot', 'Capture', 'Text' if PlotMode == 'LivePlot': # Some settings are necessary to enable ion() # a plot that is updated as the measurements fig = figure() # proceed. Note that this takes significant ax = fig.add_subplot(111) # processing resources and slows the radar down. line1, = ax.plot(np.real(a)) xlabel('Doppler frequency (Hz)') ylabel('Amplitude spectrum') xlim(-2000, 2000) ylim(0, 1) # Infinite measurement loop, break by ctrl-c or set PlotMode='Capture' while True: starttime = datetime.datetime.now() # Start time for n in range(0, N): # Sample N values I = adcdac.read_adc_voltage(1, 0) # Read I signal from pin 1 Q = adcdac.read_adc_voltage(2, 0) # Read Q signal from pin 2 a[n] = I + 1j*Q # Save as analytic signal endtime = datetime.datetime.now() # Stop time T = (endtime - starttime).total_seconds() # Compute total time m = mean(a) # Estimate the mean a_f = fftshift(fft(a - m)) # Compute Fourier transform for zero mean fvec = fftshift(fftfreq(N, d=T/N)) # Compute frequencies f = fvec[abs(a_f) == max(abs(a_f))][0] # Find frequency of maximum signal if PlotMode == 'LivePlot': # Update plot line1.set_xdata(fvec) line1.set_ydata(abs(a_f)) fig.canvas.draw() elif PlotMode == 'Capture': # Make some plots and exit figure() # Plots can be saved using GUI plot(linspace(0, T, len(a)), real(a), 'b-') plot(linspace(0, T, len(a)), imag(a), 'r--') xlabel('Time (s)') ylabel('Raw analytic signal') figure() plot(linspace(0, T, len(a)), real(a-m), 'b-') plot(linspace(0, T, len(a)), imag(a-m), 'r--') xlabel('Times (s)') ylabel('Zero mean analytic signal') figure() plot(fvec, abs(a_f)) xlabel('Doppler frequency (Hz)') ylabel('Amplitude spectrum') show() exit() print('Doppler frequency = {0:10.2f} Hz'.format(f)) # Print Doppler frequency of maximum signal