|
|
#!/usr/bin/env python
|
|
|
#-*- coding: utf-8 -*-
|
|
|
"""Simple python library to compute transfert functions
|
|
|
"""
|
|
|
import time
|
|
|
import sys
|
|
|
import os
|
|
|
import matplotlib.pyplot as plt
|
|
|
import numpy as np
|
|
|
from scipy import fftpack
|
|
|
|
|
|
__author__ = "Alexis Jeandet"
|
|
|
__copyright__ = "Copyright 2015, Laboratory of Plasma Physics"
|
|
|
__credits__ = []
|
|
|
__license__ = "GPLv2"
|
|
|
__version__ = "1.0.0"
|
|
|
__maintainer__ = "Alexis Jeandet"
|
|
|
__email__ = "alexis.jeandet@member.fsf.org"
|
|
|
__status__ = "Development"
|
|
|
|
|
|
|
|
|
def __parseFFT(FFTi,FFTo,signalFreq,samplingFreq):
|
|
|
index=signalFreq*len(FFTi)/samplingFreq
|
|
|
powI=np.abs(FFTi[int(index-1):int(index+1)])
|
|
|
i=int(np.argmax(powI)+index-1)
|
|
|
mod=np.abs(FFTo[i])/np.abs(FFTi[i])
|
|
|
arg=np.angle(FFTo[i])-np.angle(FFTi[i])
|
|
|
if arg<-np.pi:
|
|
|
arg = (np.pi*2)+arg
|
|
|
if arg>np.pi:
|
|
|
arg = (-np.pi*2)+arg
|
|
|
return [signalFreq,mod,arg]
|
|
|
|
|
|
def __compute_params(device,freq):
|
|
|
Periods=5
|
|
|
Fs=(freq*device.max_sampling_buffer)/Periods
|
|
|
if Fs < device.min_sampling_freq :
|
|
|
return [device.min_sampling_freq,device.max_sampling_buffer]
|
|
|
while Fs > (0.98*device.max_sampling_freq) :
|
|
|
Periods=Periods+1
|
|
|
Fs=(freq*device.max_sampling_buffer)/Periods
|
|
|
return [Fs,device.max_sampling_buffer]
|
|
|
|
|
|
def __step(device,freq,offset=0.0,maxAmp=5.0,lastAmp=1.0):
|
|
|
device.analog_out_gen(freq, shape='Sine', channel=0, amplitude=lastAmp, offset=offset)
|
|
|
params=__compute_params(device,freq)
|
|
|
res=device.analog_in_read(ch1=True,ch2=True,frequency=params[0],samplesCount=params[1],ch1range=5.0,ch2range=5.0)
|
|
|
meanI=np.mean(res[0][0])
|
|
|
meanO=np.mean(res[0][1])
|
|
|
FFTi=fftpack.fft((res[0][0]-meanI)*np.hamming(len(res[0][0])))
|
|
|
FFTo=fftpack.fft((res[0][1]-meanO)*np.hamming(len(res[0][1])))
|
|
|
return __parseFFT(FFTi,FFTo,freq,res[1])
|
|
|
|
|
|
def generateLogFreq(startFreq=1.0,stopFreq=100.0,nstep=100):
|
|
|
freq=np.zeros(nstep)
|
|
|
for i in range(int(nstep)) :
|
|
|
freq[i]=startFreq*np.power(10,((np.log10(stopFreq/startFreq))*i/(nstep-1)))
|
|
|
return freq
|
|
|
|
|
|
def computeTF(device,startFreq=1.0,stopFreq=100.0,offset=0.0,maxAmp=5.0,nstep=100):
|
|
|
freqs=generateLogFreq(startFreq,stopFreq,nstep)
|
|
|
f=[]
|
|
|
mod=[]
|
|
|
arg=[]
|
|
|
lastAmp=0.1
|
|
|
for freq in freqs:
|
|
|
step=__step(device,freq,offset=offset,maxAmp=maxAmp,lastAmp=lastAmp)
|
|
|
f.append(step[0])
|
|
|
mod.append(step[1])
|
|
|
arg.append(step[2])
|
|
|
return [f,mod,arg]
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
print("")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|