import math import scipy import scipy.fftpack import scipy.integrate import fileManager import evaluator import sys import os os.environ['MPLCONFIGDIR']='/tmp' import pylab from mod_python import apache def cablemagic(formula, startVal, stopVal, numBins, supplyOutput, attenDB, attenFreqMHz, cableLength, debug=False): if(startVal" % formula # get a file manager and a temporary directory fm = fileManager.fileManager('/var/www/html/cable-magic/data', fileTimeOut=3600) dir = fm.getDir() # ----------------------------------------------------------- # user supplied function eval = evaluator.Evaluator(formula, "t", xrange) xdata, ydata = eval.getData() # get the length of the data and figure out the timestep N = len(xdata) timestep = math.fabs(max(xdata)-min(xdata))/N # graph the origional function # origional function pylab.suptitle('Input Function', fontsize=16) pylab.subplot(211) origRealFunc = pylab.plot(xdata, ydata.real) pylab.title("Real Part", fontsize=12) pylab.ylabel("Amplitude") pylab.subplot(212) origImagFunc = pylab.plot(xdata, ydata.imag) pylab.title("Imag Part", fontsize=12) pylab.xlabel("Time") pylab.ylabel("Amplitude") origFuncPath = os.path.join(dir, "orig.png") origFuncPDFPath = os.path.join(dir, "orig.pdf") pylab.savefig(origFuncPath) pylab.savefig(origFuncPDFPath) pylab.clf() # integrate abs(input)^2 over time inputIntegral = scipy.integrate.trapz(abs(ydata)**2.0, dx=timestep) # bob's scaling factor #scaleFactor = N/(math.fabs(max(xdata)-min(xdata))) scaleFactor = (math.fabs(max(xdata)-min(xdata)))/N # get the input sum after scaling inputSum = sum(abs(ydata)**2.0) ydata = ydata * scaleFactor # --------------------------------------------------------- # get the fft of the user supplied function fourier = scipy.fft(ydata) freqs = scipy.fftpack.fftfreq(N, d = timestep) inputFFTSum = 1.0/N * sum(abs(fourier/scaleFactor)**2.0) freqstep = math.fabs(max(freqs)-min(freqs))/N # plot the fft of the origional function x = scipy.fftpack.fftshift(freqs) y = scipy.fftpack.fftshift(fourier) origFFTy = y pylab.plot(x, abs(y)**2) pylab.title("Input Power Spectrum", fontsize=16) pylab.xlabel("Frequency") pylab.ylabel("Power") fftFuncPath = os.path.join(dir, "fft.png") fftFuncPDFPath = os.path.join(dir, "fft.pdf") pylab.savefig(fftFuncPath) pylab.savefig(fftFuncPDFPath) pylab.clf() # ----------------------------------------------------------------------- atten0 = attenDB * 1.151 * math.pow(10.0, -3) attenFreqGHz = attenFreqMHz/1000.0 phi = ((cableLength * atten0) / math.sqrt(attenFreqGHz)) respFunc = "exp(-1.0*%f * sqrt(abs(f)))*cos(%f * sqrt(abs(f))) - j * ( exp(-1.0*%f*sqrt(abs(f))) * sin(%f * sqrt(abs(f))))" % ( phi, phi, phi, phi) if(debug): print "response function: %s
" % respFunc respEval = evaluator.Evaluator(respFunc, "f", freqs) respXData, respYData = respEval.getData() dataLen = len(respXData) i=0 while(i" print "attenFound: ", attenFound, "
" print "attenFreqFound: ", attenFreqFound, "
" print "cableLenFound: ", cableLenFound, "
" print "startValFound: ", startValFound, "
" print "stopValFound: ", stopValFound, "
" print "numBinsFound: ", numBinsFound, "
" print "supplyOutputFound: ", supplyOutputFound, "
" print "formulaFound: ", formulaFound, "
" return if(debug): print "Formula: %s
" % formula try: data = cablemagic(formula, startVal, stopVal, numBins, supplyOutput, atten, attenFreq, cableLen, debug) print data except Exception, e: print "Error: ", e