Rename signal -> signals

This commit is contained in:
Eryn Wells 2018-03-26 21:51:24 -04:00
parent 1d611f5a70
commit 61a21ff4be
3 changed files with 0 additions and 0 deletions

7
signals/__init__.py Normal file
View file

@ -0,0 +1,7 @@
# __init__.py
# Eryn Wells <eryn@erynwells.me>
from . import waves
from . import domain
__all__ = ['domain', 'waves']

41
signals/domain.py Normal file
View file

@ -0,0 +1,41 @@
# domain.py
# Eryn Wells <eryn@erynwells.me>
'''
Some utilities for dealing with signal domains: time, frequency, etc.
'''
import numpy as np
__default_time = 2.0
__default_steps = 200
class Time:
__DefaultTime = 2.0
__DefaultSteps = 200
def __init__(self, sec=__DefaultTime, samples=__DefaultSteps):
self.sec = sec
self.samples = samples
self._domain = None
@property
def domain(self):
if self._domain == None:
self._domain = np.linspace(0, self.sec, num=self.samples + 1)
return self._domain
@property
def rate(self):
'''Number of samples per second.'''
return self.samples / self.sec
@property
def interval(self):
'''Amount of time between samples, in seconds.'''
return self.sec / self.samples
def __str__(self):
return repr(self)
def __repr__(self):
return '{}(sec={}, samples={})'.format(self.__class__.__name__, self.sec, self.samples)

49
signals/waves.py Normal file
View file

@ -0,0 +1,49 @@
# waves.py
# Eryn Wells <eryn@erynwells.me>
'''
Several functions for creating audio waves.
'''
import numpy as np
__default_num_harmonics = 10
def sine(domain, freq=1.0):
'''Render a sine wave in the given domain with the specified frequency.'''
return np.sin(2.0 * np.pi * freq * domain)
def saw(domain, freq=1.0, num_harmonics=__default_num_harmonics):
'''
Render a saw wave in the given domain at the specified frequency.
This function produces a saw wave by computing the first `harmonics` terms of the
infinite series defining its shape.
'''
harmonics = np.asarray([1.0 / h * sine(domain, freq=freq * h) for h in range(1, num_harmonics)])
wave = np.sum(harmonics, axis=0)
return wave
def square(domain, freq=1, num_harmonics=__default_num_harmonics):
'''
Render a square wave in the given domain at the specified frequency.
This function produces a saw wave by computing the first `harmonics` terms of the
infinite series defining its shape.
'''
harmonics = np.asarray([1.0 / h * sine(domain, freq=freq * h) for h in range(1, num_harmonics, 2)])
wave = np.sum(harmonics, axis=0)
return wave
def triangle(domain, freq=1, num_harmonics=__default_num_harmonics):
'''
Render a triangle wave in the given domain at the specified frequency.
This function produces a saw wave by computing the first `num_harmonics` terms of the
infinite series defining its shape.
'''
def label(i):
return 2.0 * i + 1.0
harmonics = np.asarray([((-1) ** h) * (label(h) ** -2) * sine(domain, freq=freq * label(h)) for h in
range(num_harmonics)])
wave = np.sum(harmonics, axis=0)
return wave