-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.py
87 lines (71 loc) · 2.8 KB
/
generator.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# -*- coding: utf-8 -*-
from Tkinter import Tk, Frame, Scale, Checkbutton, IntVar
from math import cos, sin, pi
class Generator(Frame):
"""
Vibration harmonique du type : e=a*sin(2*pi*f*t+p)
scale_A : controleur d'Amplitude
scale_F : controleur de Frequence
scale_P : controleur de Phase
"""
def __init__(self, parent, name="X"):
"""
Initialisation
parent : un oscilloscope
name : nom du signal
"""
Frame.__init__(self)
self.configure(bd=1, relief="sunken")
self.parent = parent
self.name = name
self.drawVar = IntVar()
self.signal = None
self.draw = Checkbutton(self,text="Afficher "+self.name, selectcolor=eval('self.parent.view.color_'+name), variable=self.drawVar, onvalue = 1, offvalue = 0, command=self.parent.plot_all)
self.draw.pack()
self.draw.select()
self.scale_A = Scale(self, length=100, orient="horizontal",
label=name + " Amplitude", showvalue=1, from_=0, to=10,
tickinterval=1, command=self.update_signal)
self.scale_A.pack(expand="yes", fill="both")
self.scale_F = Scale(self, length=100, orient="horizontal",
label=name + " Fréquence", showvalue=1, from_=1, to=10,
tickinterval=1, command=self.update_signal)
self.scale_F.pack(expand="yes", fill="both")
self.scale_P = Scale(self, length=100, orient="horizontal",
label=name + " Phase", showvalue=1, from_=0, to=10,
tickinterval=1, command=self.update_signal)
self.scale_P.pack(expand="yes", fill="both")
self.bind("<Configure>",self.update_signal)
def update_signal(self, event):
"""
Mise a jour de signal si modifications (amplitude, frequence, phase)
"""
print("Vibration.update_signal()")
print("Amplitude :", self.scale_A.get())
scaling=0.05
amp = scaling*self.scale_A.get()
signal = self.generate_signal(a=amp,f=self.scale_F.get(),
p=self.scale_P.get())
self.signal = signal
if not isinstance(self.parent, Tk):
self.parent.update_view(self.name, signal)
self.parent.plot_all()
return signal
def generate_signal(self, a=1.0, f=2.0, p=0):
"""
Calcul de l'elongation : e=a*sin(2*pi*f*t+p) sur une periode
a : amplitude
f : frequence
p : phase
"""
signal = []
samples = 1000
for t in range(0, samples):
samples = float(samples)
e = a * sin((2*pi*f*(t/samples)) - p)
signal.append((t/samples,e))
return signal
if __name__ == "__main__":
root = Tk()
Generator(root).pack()
root.mainloop()