generate_rect.py


1
#! /usr/bin/env python3
2
3
4
from scipy import signal
5
import numpy as np
6
import matplotlib.pyplot as plt
7
8
count = 6
9
10
sample_rate = 44100
11
freq = 1000
12
timescale = 0.002
13
14
samples = np.arange( timescale * sample_rate) / sample_rate
15
16
def generate_sine( factor = 1):
17
    amplitude = 1.0 / factor
18
    frequency = freq * factor
19
    return amplitude  *  np.sin( 2 * np.pi * samples * frequency)
20
21
def generate_rect( max_harmonic = 1):
22
    harmonic = 1
23
    signal = generate_sine( harmonic)
24
    while harmonic < max_harmonic:
25
        harmonic += 2
26
        signal += generate_sine( harmonic)
27
    return signal
28
29
30
fig = plt.figure(1, figsize=( 6, count * 3))
31
fig.subplots_adjust( hspace = 0.5)
32
33
hoehe, breite = count, 1
34
for index in range( count):
35
    harmonic = index * 2 + 1
36
    plt.subplot( hoehe, breite, index + 1)
37
    plt.title( 'mit %d. Oberw.' % harmonic, loc = 'right', y = 0.5)
38
    plt.step( generate_rect( harmonic), '.')
39
40
plt.show()