<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">"""
FTSanalysis.py
    Fourier Analysis Spectroscopy code

    Copyright (c) 2014 University of Toronto
    Original Version (in MatLab): 16 January 2014 by Dong Jin Shin,
            Hazem Daoud, Muhammadh Ali Abdhullah Salman, Giuseppe Castiglione
    Last Modification:  5 August 2014 by David Bailey
    Contact: David Bailey &lt;dbailey@physics.utoronto.ca&gt;
                            (http://www.physics.utoronto.ca/~dbailey)
    License: Released under the MIT License; the full terms are this license
                are appended to the end of this file, and are also available
                at http://www.opensource.org/licenses/mit-license.php.
    Note: This was translated from MatLab and has not been optimized,
            and there may be bugs in the translation.

"""

# This is written assuming it will be run in Python 2, but we would like
#    to use some Python 3 feature imported from the future.
#   In particular, want division of integers to produce a float.
from __future__ import division
import numpy
from matplotlib import pyplot

# Import intensity data for white light and HeNe laser
W_intensity = numpy.loadtxt( 'white_w.txt', unpack=True, skiprows=0)
L_intensity = numpy.loadtxt( 'hene_w.txt', unpack=True, skiprows=0)

# Subtract the intensity averages so the oscillations are around 0
W_intensity = W_intensity - numpy.mean(W_intensity)
L_intensity = L_intensity - numpy.mean(L_intensity)

# Plot raw data
rawFig = pyplot.figure()           # Create Figure
W_intensity_plot = rawFig.add_subplot(211)
W_intensity_plot.plot(W_intensity)
W_intensity_plot.set_ylabel('Intensity (Volts)');
W_intensity_plot.set_title('Raw Intensity : White light filter')
L_intensity_plot = rawFig.add_subplot(212)
L_intensity_plot.plot(L_intensity)
L_intensity_plot.set_xlabel('Bin');
L_intensity_plot.set_ylabel('Intensity(Volts)');
L_intensity_plot.set_title('Raw Intensity: HeNe Laser')
rawFig.show()

##Search for zero point crossings
#    first find the array indices of any exact zeros (Laser Light)
L_zeros = numpy.where( L_intensity == 0. )

# Now add zero crossings between data points (Laser Light), by finding
#   indices of negative values that are followed by positive ones or vice versa
L_zeros = numpy.append(
                    L_zeros,
                    numpy.where( (L_intensity[1:-1] * L_intensity[2:]) &lt; 0 ))
# Sort indices so they are in increasing order
L_zeros = numpy.sort(L_zeros)

# Get "fractional" numbers of sample points at which the zeroes are
# estimated to occur through linear interpolation (Laser Light)
for i in range(len(L_zeros)) :
    if (L_intensity[L_zeros[i]]&lt;0 and L_intensity[L_zeros[i]+1]&gt;0 ) :
    # linear interpolation to get fractional number of sample points
    #         corresponding to zero crossing
	L_zeros[i] = ( L_zeros[i] +
                    (abs(L_intensity[L_zeros[i]]))/(abs(L_intensity[L_zeros[i]])
	            +abs(L_intensity[L_zeros[i]+1])))
    elif (L_intensity[L_zeros[i]]&gt;0 and L_intensity[L_zeros[i]+1]&lt;0) :
        L_zeros[i] = ( L_zeros[i] +
                    (abs(L_intensity[L_zeros[i]]))/(abs(L_intensity[L_zeros[i]])
                    +abs(L_intensity[L_zeros[i]+1])))
    elif (L_intensity[L_zeros[i]]&lt;0 and L_intensity[L_zeros[i]-1]&gt;0) :
        L_zeros[i] = ( L_zeros[i] -
                    (abs(L_intensity[L_zeros[i]]))/(abs(L_intensity[L_zeros[i]])
                    +abs(L_intensity[L_zeros[i]-1])))
    elif (L_intensity[L_zeros[i]]&gt;0 and L_intensity[L_zeros[i]-1]&lt;0) :
        L_zeros[i] = ( L_zeros[i] -
                    (abs(L_intensity[L_zeros[i]]))/(abs(L_intensity[L_zeros[i]])
                    +abs(L_intensity[L_zeros[i]-1])))

# Now L_zeros contains non-integer values of sample points
#    corresponding to zero point crossings

## Assign phases to all points
#    Assign phases to points lying between the first and last zero crossings
first = (numpy.ceil(L_zeros[0])).astype(int)
last  = (numpy.ceil(L_zeros[-1])-1).astype(int)

# Create a vector of associated phases (Laser Light)
L_phase = numpy.zeros(last-first+1)
for j in range(1,len(L_zeros)) :
    for i in range(first,last) :
        if i&gt;L_zeros[j] :
            j=j+1;
            break
        if  i&gt;=L_zeros[j-1] and L_zeros[j]&gt;=i :
            if (L_zeros[j-1]&gt;i-1 and i != first) :
                L_phase[i-first+1] = ( L_phase[i-first] +
                    ((i-L_zeros[j-1])/(L_zeros[j]-L_zeros[j-1]))*numpy.pi
                    +((L_zeros[j-1]-(i-1))/(L_zeros[j-1]-L_zeros[j-2]))*numpy.pi )
            elif (L_zeros[j-1]&gt;i-1 and i == first) :
                L_phase[i-first+1] = ( L_phase[i-first+1] +
                    ((i-L_zeros[j-1])/(L_zeros[j]-L_zeros[j-1]))*numpy.pi )
            elif (L_zeros[j-1]&lt;=i-1) :
                L_phase[i-first+1] = ( L_phase[i-first] +
                    (1/(L_zeros[j]-L_zeros[j-1]))*numpy.pi )

# Define segments of equal phase
#     Phase vector is the same length as the actual intensity vector to prevent
#         FFT problems.
phases = numpy.linspace(L_phase[0], L_phase[-1],len( W_intensity[first:last])+1)

# Fit points using cubic spline interpolation (White Light)
New_W_intensity = W_intensity[first:last+1]
from scipy.interpolate import UnivariateSpline as spline
W_proper = spline(L_phase,New_W_intensity)(phases)

##FTS part

# Subtract average of the intensity to zero I
I = W_proper - numpy.mean(W_proper)
print(len(I),I)

# Filter out 0Thz to 100Thz frequency range

fs = 5000
T = 1/fs
V = 0.0499942e-3;
D = T*V;
Tao = D/3e8;
fs_tao = 1/Tao;
nyquist = fs_tao/(2*1e12) # nyquist frequency (THz)

#m = len(I);          # Filter window length
#n = pow2(1.5*nextpow2(m))  # Transform length
y = numpy.fft.fft(I);           # DFT
print(len(y),y)
#f = (-n/2:n/2-1)*(fs_tao/n)/1e12;     % Frequency range (THz)
#nm = (1./(f*1e12))*3e8*1e9;  %wavelength range (nm)
#wl = ((1./f)*3e8)*1e-9; %wavelength (nm)
#power = abs(y)/n;   % Power of the DFT
#power = power/max(power);
#
fftFig = pyplot.figure()           # Create Figure
IntensityPlot = fftFig.add_subplot(211)
IntensityPlot.plot(I)
IntensityPlot.set_xlabel('Bin');
IntensityPlot.set_ylabel('Intensity(V)');
IntensityPlot.set_title('Intensity(V) vs Bin: White light filter, Fs = 10kHz')
TransmissionPlot = fftFig.add_subplot(212)
#plot(nm,fftshift(power));
TransmissionPlot.axis([200,900,0,1]);
TransmissionPlot.set_xlabel('Wavelength (nm)');
TransmissionPlot.set_ylabel('Transmission');
TransmissionPlot.set_title('Transmission vs Wavelength (nm)')
fftFig.show()

"""
Full text of MIT License:

    Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
</pre></body></html>