Exemple #1
0
def main():
    filename = str(input("Input file : "))  # Get filename
    data = f.readCSV(filename)  # Read in data to array

    x = data[0]  # set x/y to first cols
    y = data[1]
    if data.shape[0] > 2:  # If three cols use errors
        yErr = data[2]  # set yErr to column 2
    else:
        yErr = None  # Set to None

    # Do a fit using curve_fit, note retuns two lists.
    popt, pcov = curve_fit(line, x, y, sigma=yErr)
    perr = np.sqrt(np.diag(pcov))  # Errors

    #    Print out the results, popt contains fitted vales and perr the errors
    print("a is : {0:10.5e} +/- {1:10.5e}".format(popt[0], perr[0]))
    print("b is : {0:10.5e} +/- {1:10.5e}".format(popt[1], perr[1]))
    print("c is : {0:10.5e} +/- {1:10.5e}".format(popt[2], perr[2]))

    plt.errorbar(x, y, xerr=0.0, yerr=yErr, fmt="bx")  # Plot the data
    #      Plot the line
    plt.plot(x, line(x, *popt), "r")

    #       Label the graph
    plt.title("Exp Fit a: {0:8.4g} b: {1:8.4g}".format(popt[0], popt[1]))
    plt.xlabel("x value")
    plt.ylabel("y value")
    plt.show()
Exemple #2
0
def main():

    #       Get filenamme
    filename = str(input("Input file : "))
    # Read to t,x numpy arrays using tab seperator and skipping the
    #  header of five lines.
    t,x = f.readCSV(filename,(True,True,False),separ="\t",headerskip=5)

    #      Plot raw data in upper pannel
    dt = t[1] - t[0]
    plt.subplot(2,1,1)
    plt.plot(t,x)
    plt.title("Input Data")
    plt.xlabel("Sample is : " + str(dt))

    #      Filter input signal by a Hamming window to reduce ailiasing
    hamming = sig.hamming(t.size)
    y = x*hamming

    #       Take fft, shift to centre and take modulus
    cft = np.fft.fft(y)
    cft = np.fft.fftshift(cft)
    mod = abs(cft)

    #         Build linear space for display
    dw = 1.0/(t.size*dt)
    w = np.linspace(-0.5/dt,0.5/dt,t.size)

    #         Plot mof of FT in lower window.
    plt.subplot(2,1,2)
    plt.plot(w,mod)
    plt.xlabel("Sample is : " + " {0:7.3e}".format(dw))

    
    plt.show()
Exemple #3
0
def main():
    m,t = f.readCSV(input("File : "))
    k = 4.0*math.pi**2 * m/t**2
    print(k)

    kmean = np.mean(k)
    kerr = np.std(k)/math.sqrt(k.size)

    print("Value of k is : {0:5.2f}  +/-  {1:4.2f}".format(kmean,kerr))
Exemple #4
0
def main():
    filename = str(input("Input file : "))  # Get filename
    data = f.readCSV(filename)  # Read in data to array

    x = data[0]  # set x/y to first cols
    y = data[1]

    if data.shape[0] > 2:  # If three cols use errors
        yErr = data[2]  # set yErr to column 2
    else:
        yErr = None  # Set to None

    #      Estimate of peak location and height
    peakheight = np.amax(y)  # Peak value
    peakloc = x[np.argmax(y)]  # Peak locaion

    # Do a fit using curve_fit, note retuns two lists.
    popt, pcov = curve_fit(gauss,
                           x,
                           y,
                           p0=[peakheight, peakloc, 1.0, 0.0],
                           sigma=yErr)

    perr = np.sqrt(np.diag(pcov))  # Errors

    #    Print out the results, popt contains fitted vales and perr the errors
    print("a is : {0:10.5e} +/- {1:10.5e}".format(popt[0], perr[0]))
    print("b is : {0:10.5e} +/- {1:10.5e}".format(popt[1], perr[1]))
    print("c is : {0:10.5e} +/- {1:10.5e}".format(popt[2], perr[2]))
    print("d is : {0:10.5e} +/- {1:10.5e}".format(popt[3], perr[3]))

    plt.subplot(2, 1, 1)
    plt.errorbar(x, y, xerr=0.0, yerr=yErr, fmt="bx")  # Plot the data
    #      Plot the
    plt.plot(x, gauss(x, *popt), "r")

    #       Label the graph
    plt.title("Guassian Fit pk: {0:8.4g} loc: {1:8.4g} w: {2:8.4g}".format(
        popt[0], popt[1], popt[2]))
    plt.xlabel("x value")
    plt.ylabel("y value")

    #    Add residuals
    plt.subplot(2, 1, 2)
    plt.errorbar(x, gauss(x, *popt) - y, xerr=0.0, yerr=yErr, fmt="rx")
    plt.xlabel("x value")
    plt.ylabel("y residual")
    plt.show()
Exemple #5
0
def main():

    filename = str(input("File : "))  # Read file name as str
    data = f.readCSV(filename)  # Default csv read to array on np.array

    if data.shape[0] > 2:  # If three cols use errors
        yErr = data[2]  # set yErr to column 2
    else:
        yErr = None  # Set to None

    #        Plot out data with errors bars and sensible titles.
    plt.errorbar(data[0], data[1], xerr=0.0, yerr=yErr, fmt="bx")
    plt.title("Data Plot: {0:s}".format(filename))
    plt.xlabel("x value")
    plt.ylabel("y value")
    plt.show()
def main():

    file = tio.getFilename("File","txt")   # Open File
    wave,ref = csv.readCSV(file)           # Read to np arrays

    index = Sellmeier(1.25,0.1).fitIndex(wave,ref)
                          
    print("Index : " + repr(index))

    print("Nd : " + str(index.getNd()) + " Vd : " + str(index.getVd()))
    dn = index.getDerivative(Sodium_D)
    print("Derivative is : " + str(dn))
    
    plt.plot(wave,ref,"x")
    
    index.draw()
    plt.show()
Exemple #7
0
def main():

    # read in csv file and plot
    file = t.openFile("Data", "r", "txt")
    xpos, intensity = csv.readCSV(file)
    peak = (xpos[0] + xpos[-1]) / 2

    t.tprint("Number of data points : ", xpos.size)
    t.tprint("X range is : ", xpos[0], " to ", xpos[-1])

    width = t.getFloat("Slit width", 0.1)
    separ = t.getFloat("Slit seperation", 0.4)
    peak = t.getFloat("Peak", peak)
    pintensity = t.getFloat("Peak Intensity", 1.0)

    # Make a guess od the slits
    slits = FitSlits(separ, width, distance, peak, wave, pintensity, 0.0)

    #     Do a curve fit wit p0 v=being the initial giess
    popt,pcov = curve_fit(slits.line,xpos,intensity,\
                          p0=[slits.separ,slits.width,slits.peak,slits.intensity,slits.offset])
    perr = np.sqrt(np.diag(pcov))
    # print(popt)
    # print(perr)

    #      Print out the results
    t.tprint("Separation: {0:7.5f} +/- {1:7.5}".format(popt[0], perr[0]))
    t.tprint("Slit width: {0:7.5f} +/- {1:7.5}".format(popt[1], perr[1]))
    t.tprint("Peak centre: {0:7.5f} +/- {1:7.5}".format(popt[2], perr[2]))
    t.tprint("Peak intensity: {0:7.5f} +/- {1:7.5}".format(popt[3], perr[3]))
    t.tprint("Offset: {0:7.5f} +/- {1:7.5}".format(popt[4], perr[4]))

    #    Plot outputs
    plt.plot(xpos, intensity, 'o')
    xfine = np.linspace(xpos[0], xpos[-1], 500)  # do a fine plot as comparison
    plt.plot(xfine, slits.getArrayValues(xfine))
    plt.ylim(0.0, slits.intensity)
    plt.title("Fit of Slit Data")
    plt.xlabel("X position")
    plt.ylabel("Intensity")

    plt.show()
def main():

    td, = f.readCSV(input("Input file : "))
    print("Length is " + str(td.size))
    mean = np.mean(td)
    std = np.std(td)
    em = std / math.sqrt(td.size)
    print("Mean is : " + str(mean) + " and STD is " + str(std))
    print("Error on mean is : " + str(em))
    plt.hist(td, 50)
    plt.plot([mean, mean], [0, 50], "r")
    plt.plot([mean - std, mean - std], [0, 50], "g")
    plt.plot([mean + std, mean + std], [0, 50], "g")
    plt.plot([mean - 2 * std, mean - 2 * std], [0, 20], "b")
    plt.plot([mean + 2 * std, mean + 2 * std], [0, 20], "b")
    plt.plot([mean - 3 * std, mean - 3 * std], [0, 10], "b")
    plt.plot([mean + 3 * std, mean + 3 * std], [0, 10], "b")
    plt.title("Mean : {0:6.4f} sd: {1:6.4f} em: {2:6.4f}".format(
        mean, std, em))
    plt.show()
def main():
    temp,time = f.readCSV(input("File : "))

    plt.errorbar(temp,time,2.0,1.0,fmt = "o")
    plt.show()
def main():

    data_file = input("Input file : ")
    C0_freq, C1_freq, C0_ampl, C1_ampl, C0_phase, C1_phase, phdiff = f.readCSV(
        data_file)

    #limiting value
    first = C0_freq[0]
    #limiting index
    limit = [0]

    #find on which index value changes
    for i, element in enumerate(C0_freq):
        if (element - 0.3) > first:
            print(element)
            first = C0_freq[i]
            limit.append(i)

    limit_2 = limit[1:]

    print(limit_2)

    #copies of arrays
    C0_freq_copy = C0_freq
    C0_freq = []
    C1_freq_copy = C1_freq
    C1_freq = []

    C0_ampl_copy = C0_ampl
    C0_ampl = []
    C1_ampl_copy = C1_ampl
    C1_ampl = []

    C0_phase_copy = C0_phase
    C0_phase = []
    C1_phase_copy = C1_phase
    C1_phase = []
    phdiff_copy = phdiff
    phdiff = []

    print(limit_2[0])
    start = 0

    #delete arrays smaller than specific length
    for i, element in enumerate(limit_2):
        if len(C0_freq_copy[start:element]) >= 5:
            C0_freq.append(C0_freq_copy[start:element])
            C1_freq.append(C1_freq_copy[start:element])
            C0_ampl.append(C0_ampl_copy[start:element])
            C1_ampl.append(C1_ampl_copy[start:element])
            C0_phase.append(C0_phase_copy[start:element])
            C1_phase.append(C1_phase_copy[start:element])
            phdiff.append(phdiff_copy[start:element])

        start = element

    #Check how many elements maximum and minimum arrays have
    longest_array_length = max(len(C0_freq[i]) for i in range(0, len(C0_freq)))
    shortest_array_length = min(
        len(C0_freq[i]) for i in range(0, len(C0_freq)))
    print("Longest array: " + str(longest_array_length))
    print("Shortest array: " + str(shortest_array_length))
    print("Length of C0_array: " + str(len(C0_freq)))

    #slice arrays to make them all of the same length
    for i, el in enumerate(C0_freq):
        length = len(C0_freq[i])

        if length > shortest_array_length:
            diff = length - shortest_array_length
            if diff == 1:
                C0_freq[i] = C0_freq[i][1:]
                C1_freq[i] = C1_freq[i][1:]
                C0_ampl[i] = C0_ampl[i][1:]
                C1_ampl[i] = C1_ampl[i][1:]
                C0_phase[i] = C0_phase[i][1:]
                C1_phase[i] = C1_phase[i][1:]
                phdiff[i] = phdiff[i][1:]
            else:
                C0_freq[i] = C0_freq[i][diff - 1:-1]
                C1_freq[i] = C1_freq[i][diff - 1:-1]
                C0_ampl[i] = C0_ampl[i][diff - 1:-1]
                C1_ampl[i] = C1_ampl[i][diff - 1:-1]
                C0_phase[i] = C0_phase[i][diff - 1:-1]
                C1_phase[i] = C1_phase[i][diff - 1:-1]
                phdiff[i] = phdiff[i][diff - 1:-1]

    #copies of arrays
    R = 10  #resistance

    C0_freq_copy = C0_freq
    C0_freq = [[] for i in C0_freq_copy]
    C1_freq_copy = C1_freq
    C1_freq = [[] for i in C0_freq_copy]

    C0_ampl_copy = C0_ampl
    C0_ampl = [[] for i in C0_freq_copy]
    C1_ampl_copy = C1_ampl
    C1_ampl = [[] for i in C0_freq_copy]

    C0_phase_copy = C0_phase
    C0_phase = [[] for i in C0_freq_copy]
    C1_phase_copy = C1_phase
    C1_phase = [[] for i in C0_freq_copy]

    phdiff_copy = np.sin(np.radians(np.array(phdiff) * (-1)))
    phdiff = [[] for i in C0_freq_copy]
    print("AAAAa" + str(phdiff_copy))

    Z_copy = [
        C1_ampl_copy[i] / C0_ampl_copy[i] * R
        for i in range(0, len(C0_ampl_copy))
    ]
    Z = [[] for i in C0_freq_copy]

    data_copy = [
        C0_freq_copy, C1_freq_copy, C0_ampl_copy, C1_ampl_copy, C0_phase_copy,
        C1_phase_copy, phdiff_copy, Z_copy
    ]
    data = [C0_freq, C1_freq, C0_ampl, C1_ampl, C0_phase, C1_phase, phdiff, Z]

    names = [
        "Mean", "Standard Dev", "Standard Err", "Variance", "Max", "Min",
        "Median", "Skewness", "Kurtosis"
    ]

    def skewness(mean, std, array):
        x = np.sum([(((i - mean) / std)**3) for i in array])
        result = (1 / len(array)) * x
        return result

    def kurtosis(mean, std, array):
        x = np.sum([(((i - mean) / std)**4) for i in array])
        result = (1 / len(array)) * x - 3  #excess kurtosis
        return result

    print(np.matrix(data_copy[0]))
    for i, el in enumerate(data_copy):
        for j, el2 in enumerate(data_copy[i]):
            data[i][j].append(np.mean(data_copy[i][j]))
            data[i][j].append(np.std(data_copy[i][j], ddof=1))
            data[i][j].append(
                np.std(data_copy[i][j], ddof=1) /
                math.sqrt(data_copy[i][j].size))
            data[i][j].append((np.std(data_copy[i][j], ddof=1))**2)
            data[i][j].append(np.max(data_copy[i][j]))
            data[i][j].append(np.min(data_copy[i][j]))
            data[i][j].append(np.median(data_copy[i][j]))
            data[i][j].append(
                skewness(np.mean(data_copy[i][j]),
                         np.std(data_copy[i][j], ddof=1), data_copy[i][j]))
            data[i][j].append(
                kurtosis(np.mean(data_copy[i][j]),
                         np.std(data_copy[i][j], ddof=1), data_copy[i][j]))

    print(data[6][0])
    print("Z vertes: " + str(data[-1][0]))
    #calculating impedence:
    R = 10  #resistance ohm

    Z = [data[3][i][0] * R / data[2][i][0] for i in range(0, len(data[3]))]
    print("!!!!: " + str(len(Z)))

    freq = np.array(data[1])
    print("nu ziurim cia: " + str(names))

    #save data to csv file:
    np.savetxt("sin with mass.csv", data[6], delimiter=",")

    frequencies = freq[:, 0]
    print("-----")
    print("frequencies " + str(frequencies))
    print("standard error on frequency " + str(freq[:, 2]))