From 1ff777d226c09181b50a8c0c1cdf225c1a5b3b8d Mon Sep 17 00:00:00 2001 From: babetoduarte Date: Sat, 14 Oct 2017 17:03:27 -0500 Subject: [PATCH 1/4] Added basic comment descriptions to the scripts and to some of the source files. There's still much to be done, but it's a start. --- amodem/alsa.py | 3 +++ amodem/async.py | 2 ++ amodem/audio.py | 2 ++ amodem/calib.py | 2 ++ amodem/common.py | 2 ++ amodem/detect.py | 2 ++ amodem/dsp.py | 2 ++ amodem/equalizer.py | 2 ++ scripts/plot.py | 4 ++++ scripts/record.py | 3 +++ scripts/resample.py | 5 +++++ 11 files changed, 29 insertions(+) diff --git a/amodem/alsa.py b/amodem/alsa.py index 749669c..2fec92a 100644 --- a/amodem/alsa.py +++ b/amodem/alsa.py @@ -3,6 +3,9 @@ import logging log = logging.getLogger(__name__) +"""Code which adds Linux ALSA support for interfaces, +recording and playing. +""" class Interface(object): diff --git a/amodem/async.py b/amodem/async.py index 9391d5d..688dc20 100644 --- a/amodem/async.py +++ b/amodem/async.py @@ -4,6 +4,8 @@ import logging log = logging.getLogger() +"""Asynchronous Reading capabilities for amodem. +""" class AsyncReader(object): def __init__(self, stream, bufsize): diff --git a/amodem/audio.py b/amodem/audio.py index eafc728..f2d4a8c 100644 --- a/amodem/audio.py +++ b/amodem/audio.py @@ -4,6 +4,8 @@ import time log = logging.getLogger(__name__) +"""Audio capabilities for amodem. +""" class Interface(object): def __init__(self, config, debug=False): diff --git a/amodem/calib.py b/amodem/calib.py index 9473d8d..01ee34b 100644 --- a/amodem/calib.py +++ b/amodem/calib.py @@ -10,6 +10,8 @@ import subprocess log = logging.getLogger(__name__) +"""Calibration capabilities for amodem. +""" def volume_controller(cmd): def controller(level): diff --git a/amodem/common.py b/amodem/common.py index eee5415..ef863c6 100644 --- a/amodem/common.py +++ b/amodem/common.py @@ -9,6 +9,8 @@ log = logging.getLogger(__name__) scaling = 32000.0 # out of 2**15 +"""Commom utilities and procedures for amodem. +""" def load(fileobj): ''' Load signal from file object. ''' diff --git a/amodem/detect.py b/amodem/detect.py index 4f14050..069365e 100644 --- a/amodem/detect.py +++ b/amodem/detect.py @@ -9,6 +9,8 @@ import collections log = logging.getLogger(__name__) +"""Signal detection capabilities for amodem. +""" class Detector(object): diff --git a/amodem/dsp.py b/amodem/dsp.py index ce789c5..eab59db 100644 --- a/amodem/dsp.py +++ b/amodem/dsp.py @@ -2,6 +2,8 @@ import numpy as np from . import common +"""Digital Signal Processing capabilities for amodem. +""" class FIR(object): def __init__(self, h): diff --git a/amodem/equalizer.py b/amodem/equalizer.py index 9f7bd10..2ec481e 100644 --- a/amodem/equalizer.py +++ b/amodem/equalizer.py @@ -5,6 +5,8 @@ from . import levinson import numpy as np import itertools +"""Audio equalizing capabilities for amodem. +""" class Equalizer(object): diff --git a/scripts/plot.py b/scripts/plot.py index a6fc795..3546856 100755 --- a/scripts/plot.py +++ b/scripts/plot.py @@ -5,6 +5,10 @@ from amodem import common from amodem.config import Configuration import sys +"""Script that exposes pylab's spectogram plotting +capabilities to the command line. It implements this +for amodem.config Configurations. +""" def spectrogram(t, x, Fs, NFFT=256): ax1 = pylab.subplot(211) diff --git a/scripts/record.py b/scripts/record.py index 49caab9..8126f40 100755 --- a/scripts/record.py +++ b/scripts/record.py @@ -3,6 +3,9 @@ import argparse from amodem import audio from amodem.config import Configuration +"""Script that records audio through an interface +and stores it into an amodem.config Configuration. +""" def run(args): config = Configuration() diff --git a/scripts/resample.py b/scripts/resample.py index bb6ab6d..5e33a30 100755 --- a/scripts/resample.py +++ b/scripts/resample.py @@ -3,6 +3,11 @@ from amodem.sampling import resample import argparse import sys +"""Script that exposes the amodem.resample() function +to the command line, taking parameters via standard +inputs and returning results via standard outputs. +""" + def main(): p = argparse.ArgumentParser() From e1bdae2069a95285d15531f0d9d02b248503828f Mon Sep 17 00:00:00 2001 From: "Jorge A. Duarte" Date: Sun, 15 Oct 2017 13:23:57 -0500 Subject: [PATCH 2/4] Made documentation changes as requested, according to PEP-257. --- amodem/alsa.py | 8 +++++--- amodem/async.py | 4 ++-- amodem/audio.py | 5 ++--- amodem/calib.py | 4 ++-- amodem/common.py | 8 +++++--- amodem/config.py | 2 ++ amodem/detect.py | 4 ++-- amodem/dsp.py | 4 ++-- amodem/equalizer.py | 4 ++-- scripts/plot.py | 11 +++++++---- scripts/record.py | 8 +++++--- scripts/resample.py | 7 ++++--- 12 files changed, 40 insertions(+), 29 deletions(-) diff --git a/amodem/alsa.py b/amodem/alsa.py index 2fec92a..da9141e 100644 --- a/amodem/alsa.py +++ b/amodem/alsa.py @@ -1,11 +1,13 @@ +"""Code which adds Linux ALSA support for interfaces, +recording and playing. + +""" + import subprocess import logging log = logging.getLogger(__name__) -"""Code which adds Linux ALSA support for interfaces, -recording and playing. -""" class Interface(object): diff --git a/amodem/async.py b/amodem/async.py index 688dc20..13b2577 100644 --- a/amodem/async.py +++ b/amodem/async.py @@ -1,11 +1,11 @@ +"""Asynchronous Reading capabilities for amodem.""" + import threading import six # since `Queue` module was renamed to `queue` (in Python 3) import logging log = logging.getLogger() -"""Asynchronous Reading capabilities for amodem. -""" class AsyncReader(object): def __init__(self, stream, bufsize): diff --git a/amodem/audio.py b/amodem/audio.py index f2d4a8c..8fffd02 100644 --- a/amodem/audio.py +++ b/amodem/audio.py @@ -1,12 +1,11 @@ +"""Audio capabilities for amodem.""" + import ctypes import logging import time log = logging.getLogger(__name__) -"""Audio capabilities for amodem. -""" - class Interface(object): def __init__(self, config, debug=False): self.debug = bool(debug) diff --git a/amodem/calib.py b/amodem/calib.py index 01ee34b..6963775 100644 --- a/amodem/calib.py +++ b/amodem/calib.py @@ -1,3 +1,5 @@ +"""Calibration capabilities for amodem.""" + from . import common from . import dsp from . import sampling @@ -10,8 +12,6 @@ import subprocess log = logging.getLogger(__name__) -"""Calibration capabilities for amodem. -""" def volume_controller(cmd): def controller(level): diff --git a/amodem/common.py b/amodem/common.py index ef863c6..9243cd0 100644 --- a/amodem/common.py +++ b/amodem/common.py @@ -1,5 +1,7 @@ -''' Common package functionality. -''' +""" Common package functionality. +Commom utilities and procedures for amodem. + +""" import itertools import numpy as np @@ -9,7 +11,7 @@ log = logging.getLogger(__name__) scaling = 32000.0 # out of 2**15 -"""Commom utilities and procedures for amodem. +""" """ def load(fileobj): diff --git a/amodem/config.py b/amodem/config.py index abe57e2..a6881f2 100644 --- a/amodem/config.py +++ b/amodem/config.py @@ -1,3 +1,5 @@ +"""Configuration class.""" + import numpy as np diff --git a/amodem/detect.py b/amodem/detect.py index 069365e..c61ec8e 100644 --- a/amodem/detect.py +++ b/amodem/detect.py @@ -1,3 +1,5 @@ +"""Signal detection capabilities for amodem.""" + from . import dsp from . import equalizer from . import common @@ -9,8 +11,6 @@ import collections log = logging.getLogger(__name__) -"""Signal detection capabilities for amodem. -""" class Detector(object): diff --git a/amodem/dsp.py b/amodem/dsp.py index eab59db..7027625 100644 --- a/amodem/dsp.py +++ b/amodem/dsp.py @@ -1,9 +1,9 @@ +"""Digital Signal Processing capabilities for amodem.""" + import numpy as np from . import common -"""Digital Signal Processing capabilities for amodem. -""" class FIR(object): def __init__(self, h): diff --git a/amodem/equalizer.py b/amodem/equalizer.py index 2ec481e..243bc41 100644 --- a/amodem/equalizer.py +++ b/amodem/equalizer.py @@ -1,3 +1,5 @@ +"""Audio equalizing capabilities for amodem.""" + from . import dsp from . import sampling from . import levinson @@ -5,8 +7,6 @@ from . import levinson import numpy as np import itertools -"""Audio equalizing capabilities for amodem. -""" class Equalizer(object): diff --git a/scripts/plot.py b/scripts/plot.py index 3546856..fa79227 100755 --- a/scripts/plot.py +++ b/scripts/plot.py @@ -1,14 +1,17 @@ #!/usr/bin/env python + +"""Script that exposes pylab's spectogram plotting +capabilities to the command line. It implements this +for amodem.config Configurations. + +""" + import pylab import numpy as np from amodem import common from amodem.config import Configuration import sys -"""Script that exposes pylab's spectogram plotting -capabilities to the command line. It implements this -for amodem.config Configurations. -""" def spectrogram(t, x, Fs, NFFT=256): ax1 = pylab.subplot(211) diff --git a/scripts/record.py b/scripts/record.py index 8126f40..9366330 100755 --- a/scripts/record.py +++ b/scripts/record.py @@ -1,11 +1,13 @@ #!/usr/bin/env python + +"""Script that records audio through an interface +and stores it into an amodem.config Configuration. + +""" import argparse from amodem import audio from amodem.config import Configuration -"""Script that records audio through an interface -and stores it into an amodem.config Configuration. -""" def run(args): config = Configuration() diff --git a/scripts/resample.py b/scripts/resample.py index 5e33a30..9ae3873 100755 --- a/scripts/resample.py +++ b/scripts/resample.py @@ -1,13 +1,14 @@ #!/usr/bin/env python -from amodem.sampling import resample -import argparse -import sys """Script that exposes the amodem.resample() function to the command line, taking parameters via standard inputs and returning results via standard outputs. """ +from amodem.sampling import resample +import argparse +import sys + def main(): p = argparse.ArgumentParser() From 66acac3e35a7ae83338602396bd3d9fbbc0b6895 Mon Sep 17 00:00:00 2001 From: "Jorge A. Duarte" Date: Sun, 15 Oct 2017 13:52:22 -0500 Subject: [PATCH 3/4] Made PEP8 changes to several scripts and files. --- amodem/audio.py | 1 + amodem/common.py | 22 ++++++++++------------ amodem/dsp.py | 6 +++--- amodem/levinson.py | 4 ++-- scripts/plot.py | 1 + scripts/resample.py | 1 + 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/amodem/audio.py b/amodem/audio.py index 8fffd02..64cfd91 100644 --- a/amodem/audio.py +++ b/amodem/audio.py @@ -6,6 +6,7 @@ import time log = logging.getLogger(__name__) + class Interface(object): def __init__(self, config, debug=False): self.debug = bool(debug) diff --git a/amodem/common.py b/amodem/common.py index 9243cd0..32a371d 100644 --- a/amodem/common.py +++ b/amodem/common.py @@ -11,29 +11,27 @@ log = logging.getLogger(__name__) scaling = 32000.0 # out of 2**15 -""" -""" def load(fileobj): - ''' Load signal from file object. ''' + """ Load signal from file object. """ return loads(fileobj.read()) def loads(data): - ''' Load signal from memory buffer. ''' + """ Load signal from memory buffer. """ x = np.frombuffer(data, dtype='int16') x = x / scaling return x def dumps(sym): - ''' Dump signal to memory buffer. ''' + """ Dump signal to memory buffer. """ sym = sym.real * scaling return sym.astype('int16').tostring() def iterate(data, size, func=None, truncate=True, index=False): - ''' Iterate over a signal, taking each time *size* elements. ''' + """ Iterate over a signal, taking each time *size* elements. """ offset = 0 data = iter(data) @@ -51,9 +49,9 @@ def iterate(data, size, func=None, truncate=True, index=False): def split(iterable, n): - ''' Split an iterable of n-tuples into n iterables of scalars. + """ Split an iterable of n-tuples into n iterables of scalars. The k-th iterable will be equivalent to (i[k] for i in iter). - ''' + """ def _gen(it, index): for item in it: yield item[index] @@ -63,26 +61,26 @@ def split(iterable, n): def icapture(iterable, result): - ''' Appends each yielded item to result. ''' + """ Appends each yielded item to result. """ for i in iter(iterable): result.append(i) yield i def take(iterable, n): - ''' Take n elements from iterable, and return them as a numpy array. ''' + """ Take n elements from iterable, and return them as a numpy array. """ return np.array(list(itertools.islice(iterable, n))) def izip(iterables): - ''' "Python 3" zip re-implementation for Python 2. ''' + """ "Python 3" zip re-implementation for Python 2. """ iterables = [iter(iterable) for iterable in iterables] while True: yield tuple([next(iterable) for iterable in iterables]) class Dummy(object): - ''' Dummy placeholder object for testing and mocking. ''' + """ Dummy placeholder object for testing and mocking. """ def __getattr__(self, name): return self diff --git a/amodem/dsp.py b/amodem/dsp.py index 7027625..5df72b5 100644 --- a/amodem/dsp.py +++ b/amodem/dsp.py @@ -62,7 +62,7 @@ def coherence(x, omega): def linear_regression(x, y): - ''' Find (a,b) such that y = a*x + b. ''' + """ Find (a,b) such that y = a*x + b. """ x = np.array(x) y = np.array(y) mean_x = np.mean(x) @@ -100,7 +100,7 @@ class MODEM(object): yield self.encode_map[bits_tuple] def decode(self, symbols, error_handler=None): - ''' Maximum-likelihood decoding, using naive nearest-neighbour. ''' + """ Maximum-likelihood decoding, using naive nearest-neighbour. """ symbols_vec = self.symbols _dec = self.decode_list for received in symbols: @@ -113,7 +113,7 @@ class MODEM(object): def prbs(reg, poly, bits): - ''' Simple pseudo-random number generator. ''' + """ Simple pseudo-random number generator. """ mask = (1 << bits) - 1 size = 0 # effective register size (in bits) diff --git a/amodem/levinson.py b/amodem/levinson.py index 5280067..184fd6b 100644 --- a/amodem/levinson.py +++ b/amodem/levinson.py @@ -2,9 +2,9 @@ import numpy as np def solver(t, y): - ''' Solve Mx = y for x, where M[i,j] = t[|i-j|], in O(N^2) steps. + """ Solve Mx = y for x, where M[i,j] = t[|i-j|], in O(N^2) steps. See http://en.wikipedia.org/wiki/Levinson_recursion for details. - ''' + """ N = len(t) assert len(y) == N diff --git a/scripts/plot.py b/scripts/plot.py index fa79227..605deb6 100755 --- a/scripts/plot.py +++ b/scripts/plot.py @@ -34,5 +34,6 @@ def main(): pylab.show() + if __name__ == '__main__': main() diff --git a/scripts/resample.py b/scripts/resample.py index 9ae3873..363839a 100755 --- a/scripts/resample.py +++ b/scripts/resample.py @@ -17,5 +17,6 @@ def main(): resample(src=sys.stdin, dst=sys.stdout, df=args.df) + if __name__ == '__main__': main() From 555186c2d8b95cce7f27df25dee05823a874d19b Mon Sep 17 00:00:00 2001 From: "Jorge A. Duarte" Date: Sun, 15 Oct 2017 13:58:15 -0500 Subject: [PATCH 4/4] Fixed PEP-8 trailing whitespaces on doctrings. --- amodem/alsa.py | 2 +- scripts/resample.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/amodem/alsa.py b/amodem/alsa.py index da9141e..a22a561 100644 --- a/amodem/alsa.py +++ b/amodem/alsa.py @@ -1,4 +1,4 @@ -"""Code which adds Linux ALSA support for interfaces, +"""Code which adds Linux ALSA support for interfaces, recording and playing. """ diff --git a/scripts/resample.py b/scripts/resample.py index 363839a..fd597a2 100755 --- a/scripts/resample.py +++ b/scripts/resample.py @@ -1,7 +1,7 @@ #!/usr/bin/env python -"""Script that exposes the amodem.resample() function -to the command line, taking parameters via standard +"""Script that exposes the amodem.resample() function +to the command line, taking parameters via standard inputs and returning results via standard outputs. """