TTT4260/D3/python/TrekantOsc.py

57 lines
1.1 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 28 16:08:42 2019
@author: oyvind
"""
import matplotlib.pyplot as plt
freq = 1000
period = 10**3 / freq
cycles = 2
resolution = 1000
upperVolt = 1
lowerVolt = -1
def GenTime():
time = []
for t in range(2 * resolution * cycles):
time.append(t * period / (2 * resolution))
return time
def GenSquare():
square = []
for s in range(cycles):
for h in range(resolution):
square.append(upperVolt)
for h in range(resolution):
square.append(lowerVolt)
return square
def GenCenter(times):
center = []
for i in times:
center.append(0)
return center
time = GenTime()
square = GenSquare()
center = GenCenter(time)
plt.figure(figsize=(12,5))
plt.plot(time, square)
plt.hlines(0, time[0], time[-1], color='k',linestyles='dashed')
plt.title("Test")
plt.xlabel("Tid [ms]")
plt.ylabel("Spenning [V]")
plt.legend(["Pulstog, " + r'$v_1$', "Utgående spenning, " + r'$v_2$', "Terskelspenning, " + r'$V_T$'], loc="upper right")
#plt.savefig(filename + ".png", dpi = 300)
plt.show()