-
Notifications
You must be signed in to change notification settings - Fork 0
/
GEN_carrier_samples.py
104 lines (93 loc) · 4.46 KB
/
GEN_carrier_samples.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import sys
import wave
import math
import struct
import random
import argparse
import matplotlib.pyplot as plt
from itertools import *
def sine_wave(frequency=5000.0, sample_rate=44100, amplitude=0.5): #changed - framerate -> sample_rate
'''
Generate a sine wave at a given frequency of five times its time period
'''
sample_rate = 50*frequency
num_samples = int(sample_rate / frequency)
if amplitude > 1.0: amplitude = 1.0
if amplitude < 0.0: amplitude = 0.0
lookup_table = [float(amplitude) * math.sin(2.0*math.pi*float(frequency)*(float(i%num_samples)/float(sample_rate))) for i in xrange(num_samples)]
#print lookup_table[1]
for i in range(4):
for j in range(num_samples):
lookup_table.append(lookup_table[j])
time_period = 1.0/frequency
start = 0.0
step = time_period/num_samples
print step
time = []
for i in range(5*num_samples):
if start > (5*time_period):
start = (5*time_period)
time.append(start)
start = start + step
plt.subplot(111,axisbg='black')
plt.axis([0,5*time_period,(-(amplitude)-0.5),(amplitude+0.5)])
plt.plot(time, lookup_table, 'go')
plt.plot(time, lookup_table, 'g')
plt.ylabel('Amplitude');
plt.xlabel('Time');
plt.title('Carrier Waveform');
plt.text((time_period), amplitude+0.3, 'Frequency = '+str(frequency) + 'Hz', color = 'red')
plt.text((time_period), amplitude+0.2, 'Amplitude = '+str(amplitude) + 'units', color = 'red')
plt.text((2*time_period), amplitude+0.3, 'Time period = '+str(time_period) + 's', color = 'red')
plt.show()
#print lookup_table
return [lookup_table[i%num_samples] for i in range(num_samples)]#count(0)) #count(firstval = 0, step = 1) - function which can start with a first_value and increment with the step value...infinitely..
# use range fn for finite repetition
def square_wave(frequency=5000.0, sample_rate=44100, amplitude=0.5): #changed - framerate -> sample_rate
'''
Generate a square wave at a given frequency of five times its time period
'''
sample_rate = 50*frequency
num_samples = int(sample_rate / frequency)
if amplitude > 1.0: amplitude = 1.0
if amplitude < 0.0: amplitude = 0.0
lookup_table = [float(amplitude) * (-1)**(math.floor(2*i/num_samples)) for i in xrange(num_samples)]
#print lookup_table[1]
for i in range(4):
for j in range(num_samples):
lookup_table.append(lookup_table[j])
time_period = 1.0/frequency
start = 0.0
step = time_period/num_samples
print step
time = []
for i in range(5*num_samples):
if start > (5*time_period):
start = (5*time_period)
time.append(start)
start = start + step
plt.subplot(111,axisbg='black')
plt.axis([0,5*time_period,(-(amplitude)-0.5),(amplitude+0.5)])
plt.plot(time, lookup_table, 'go')
plt.plot(time, lookup_table, 'g')
plt.ylabel('Amplitude');
plt.xlabel('Time');
plt.title('Carrier Waveform');
plt.text((time_period), amplitude+0.3, 'Frequency = '+str(frequency) + 'Hz', color = 'red')
plt.text((time_period), amplitude+0.2, 'Amplitude = '+str(amplitude) + 'units', color = 'red')
plt.text((2*time_period), amplitude+0.3, 'Time period = '+str(time_period) + 's', color = 'red')
plt.show()
#print lookup_table
return [lookup_table[i%num_samples] for i in range(num_samples)]#count(0)) #count(firstval = 0, step = 1) - function which can start with a first_value and increment with the step value...infinitely..
# use range fn for finite repetition
def cycle_list(object, times=None):
# repeat([10,4], 3) --> 10 4 10 4 10 4
length = len(object)
if times is None:
for i in count(0):
yield object[i%length]
else:
for i in xrange(times):
for j in range(length):
yield object[j]
#sine_wave()