summaryrefslogtreecommitdiffstats
path: root/lab_control/frequency_response.py
blob: 84259d4add0828e3832e380456a102571e4595cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from pathlib import Path
import time

from lab_control.function_generator import FunctionGenerator
from lab_control.oscilloscope import Oscilloscope
from lab_control.measurement import Measurement, getLinearRange


class FrequencyResponseMeasurement(Measurement):
    def __init__(self):
        self.minFrequency = 20e0
        self.maxFrequency = 16e3
        self.steps = 50
        self.inputAmplitude = 0.1
        self.functionGeneratorChannel = 1
        self.oscilloscopeChannel = 1
        self.measurementDone = False
        self.data = None

    def measure(self, osc: Oscilloscope, fg: FunctionGenerator) -> None:
        frequencies = getLinearRange(self.minFrequency, self.maxFrequency, self.steps)
        self.data = []

        # Initial set up
        fg.setAmplitude(self.functionGeneratorChannel, self.inputAmplitude)
        fg.setFunction(self.functionGeneratorChannel, FunctionGenerator.SINE)
        fg.setOn(self.functionGeneratorChannel)
        osc.setVoltsPerDivision(self.oscilloscopeChannel, self.inputAmplitude / (osc.getDivisionsDisplayed() - 2))

        for f in frequencies:
            fg.setFrequency(self.functionGeneratorChannel, f)
            #time.sleep(0.2)
            response = osc.measureAmplitude(self.oscilloscopeChannel)
            self.data.append((f, response))

        self.measurementDone = True

    def dumpToCSV(self, path: Path) -> None:
        if self.data is None:
            return

        with open(path, "w") as f:
            f.write("Frequency (Hz),Amplitude (V)")

            for entry in self.data:
                f.write(f"\n{entry[0]},{entry[1]}")