Skip to main content

Generate synthetic signals for ML pipelines

Project description

hum

Generate synthetic signals for ML pipelines

To install: pip install hum

functionality

This notebook gathers various examples of the functionality of hum:

  • Synthetic datasets
    • sound-like datasets
    • diagnosis datasets
    • signal generation
  • Plotting and visualization
    • plot
    • display
    • melspectrograms
  • Infinite waveform from spectrums
  • Various sample sounds
  • Voiced time
from hum import (mk_sine_wf, 
                 freq_based_stationary_wf, 
                 BinarySound, 
                 WfGen, 
                 TimeSound, 
                 mk_some_buzz_wf, 
                 wf_with_timed_bleeps,
                 Sound,
                 plot_wf,
                 disp_wf,
                 InfiniteWaveform,
                 Voicer, 
                 tell_time_continuously,
                 random_samples,
                 pure_tone,
                 triangular_tone,
                 square_tone,
                 AnnotatedWaveform,
                 gen_words,
                 categorical_gen,
                 bernoulli_gen,
                 create_session,
                 session_to_df
                )
import matplotlib.pyplot as plt
from numpy.random import randint
import numpy as np

Synthetic datasets

There are several different forms of synthetic data that hum can produce to be used in machine learning pipelines, with the first being sound-like datasets generally in the form of sine waves

Sound-like datasets

mk_sine_wf provides an easy way to generate a simple waveform for synthetic testing purposes

DFLT_N_SAMPLES = 21 * 2048
DFLT_SR = 44100
wf = mk_sine_wf(freq=5, n_samples=DFLT_N_SAMPLES, sr=DFLT_SR, phase=0, gain=1)
plt.plot(wf);

png

wf = mk_sine_wf(freq=20, n_samples=DFLT_N_SAMPLES, sr=DFLT_SR, phase = 0.25, gain = 3)
plt.plot(wf);

png

freq_based_stationary_wf provides the ability to generate a more complex waveform by mixing sine waves of different frequencies with potentially different weights

wf_mix = freq_based_stationary_wf(freqs=(2, 4, 6, 8), weights=None,
                             n_samples = DFLT_N_SAMPLES, sr = DFLT_SR)
plt.plot(wf_mix);

png

wf_mix = freq_based_stationary_wf(freqs=(2, 4, 6, 8), weights=(3,3,1,1),
                             n_samples = DFLT_N_SAMPLES, sr = DFLT_SR)
plt.plot(wf_mix);

png

WfGen is a class that allows for the generation of sinusoidal waveforms, the generation of lookup tables to be used in generating waveforms, and frequency weighted mixed waveforms

wfgen = WfGen(sr=44100, buf_size_frm=2048, amplitude=0.5)
lookup = np.array(wfgen.mk_lookup_table(freq=880))
wf = wfgen.mk_sine_wf(n_frm=100, freq=880)
np.array(lookup).T
array([ 0.        ,  0.06252526,  0.12406892,  0.1836648 ,  0.24037727,
        0.293316  ,  0.34164989,  0.38462013,  0.42155213,  0.45186607,
        0.47508605,  0.49084754,  0.49890309,  0.49912624,  0.49151348,
        0.47618432,  0.45337943,  0.42345682,  0.38688626,  0.34424188,
        0.29619315,  0.24349441,  0.186973  ,  0.12751624,  0.06605758,
        0.00356187, -0.05898977, -0.12061531, -0.18034728, -0.23724793,
       -0.29042397, -0.33904057, -0.38233448, -0.41962604, -0.45032977,
       -0.47396367, -0.4901567 , -0.49865463, -0.49932406, -0.49215447,
       -0.47725843, -0.45486979, -0.42534003, -0.38913276, -0.34681639,
       -0.29905527, -0.2465992 , -0.19027171, -0.13095709, -0.06958655])
plt.plot(wf);

png

wf_weight = wfgen.mk_wf_from_freq_weight_array(n_frm=10000, freq_weight_array=(10,1,6))
plt.plot(wf_weight);

png

Diagnosis datasets

hum can also produce diagnosis datasets to be applied to machine learning pipelines

BinarySound is a class that generates binary waveforms

bs = BinarySound(nbits=50, redundancy=142, repetition=3, header_size_words=1)
utc = randint(0,2,50)
wf = bs.mk_phrase(utc)
plt.plot(wf[:200]);
all(bs.decode(wf) == utc)
True

png

BinarySound can also be instantiated using audio parameters using the for_audio_params class method

bs = BinarySound.for_audio_params(nbits=50, freq=6000, chk_size_frm=43008, sr=44100, header_size_words=1)
wf = bs.mk_phrase(utc)
plt.plot(wf[:200]);
all(bs.decode(wf) == utc)
True

png

utc phrases can be generated using mk_utc_phrases when BinarySound is instantiated with audio parameters

plt.plot(bs.mk_utc_phrases()[:200]);

png

TimeSound is a class that generates timestamped waveform data

time = TimeSound(sr=44100, buf_size_frm=2048, amplitude=0.5, n_ums_bits=30)
wf = time.timestamped_wf()
plt.plot(wf[2000:2300]);

png

mk_some_buzz_wf and wf_with_timed_bleeps are two more options to generate synthetic data of diagnosis sounds

wf = mk_some_buzz_wf(sr=DFLT_SR)
plt.plot(wf[:500]);

png

wf = wf_with_timed_bleeps(n_samples=DFLT_SR*2, bleep_loc=400, bleep_spec=100, sr=DFLT_SR)
plt.plot(wf[:150]);

png

Signal generation

hum can create signals generated by sequences of symbols, perturbed by outliers injected at given points

symb_res = categorical_gen(gen_words)
out_res = bernoulli_gen(p_out=0.01)
df = session_to_df(create_session(symb_res, out_res, alphabet=list('abcde'), session_length=500))
df.plot(subplots=True, figsize=(20,7));

png

Plotting and visualization

hum also provides several options for plotting and visualization for the synthetic datasets it generates

wfgen = WfGen()
wf = list()
for i in range(1, 1000, 20):
    wf.extend(list(wfgen.mk_sine_wf(n_frm=2048, freq=i)))
wf = np.array(wf)
sr = 44100

Plot waveform

plot_wf(wf[:20000], sr)

png

Display waveform

disp_wf(wf, sr)

png

Melspectrograms with Sound

snd = Sound(wf=wf, sr=sr)
snd.plot_wf(wf=wf[:20000], sr=sr)

png

snd.melspectrogram(plot_it=False)
array([[-63.34856485, -45.14910401, -36.14726097, ..., -80.        ,
        -73.35788085, -60.58728436],
       [-67.99632241, -74.80503122, -80.        , ..., -80.        ,
        -72.1600597 , -60.16803079],
       [-80.        , -80.        , -80.        , ..., -80.        ,
        -72.90050429, -60.90871386],
       ...,
       [-80.        , -80.        , -80.        , ..., -80.        ,
        -80.        , -80.        ],
       [-80.        , -80.        , -80.        , ..., -80.        ,
        -80.        , -80.        ],
       [-80.        , -80.        , -80.        , ..., -80.        ,
        -80.        , -80.        ]])
snd.display()

png

Infinite waveform from spectrum

hum also provides the functionality to create an infinite waveform based on a given spectrum, and a noise amplifier if desired

iwf = InfiniteWaveform(wf)
wf = list(iwf.query(0,500000))
disp_wf(wf)

png

Sound(wf=wf).display()

png

Sample sounds

hum also provides several functions to generate sample sounds shown below

Random sample

wf = random_samples(chk_size_frm=21*2048, max_amplitude=30000)
disp_wf(wf=wf, sr=sr)
Sound(wf=wf).display()

png

png

Pure tone sample

wf = pure_tone(chk_size_frm=21*2048, freq=440, sr=44100, max_amplitude=30000)
disp_wf(wf=wf, sr=sr)
Sound(wf=wf).display()
/Users/owenlloyd/opt/anaconda3/envs/oto3/lib/python3.8/site-packages/matplotlib/axes/_axes.py:7723: RuntimeWarning: divide by zero encountered in log10
  Z = 10. * np.log10(spec)

png

png

Triangular tone sample

wf = triangular_tone(chk_size_frm=21*2048, freq=440, sr=44100, max_amplitude=30000)
disp_wf(wf=wf, sr=sr)
Sound(wf=wf).display()

png

png

Square tone sample

wf = square_tone(chk_size_frm=21*2048, freq=440, sr=44100, max_amplitude=30000)
disp_wf(wf=wf, sr=sr)
Sound(wf=wf).display()

png

png

Annotated Waveform

awf = AnnotatedWaveform(chk_size_frm=21*2048, freq=440, sr=44100, max_amplitude=30000)
gen = awf.chk_and_tag_gen()
list(gen)
[(array([-14025,  11555,  22270, ...,  10243, -18225,   3874], dtype=int16),
  'random'),
 (array([    0,  1902,  3797, ...,  9361, 11149, 12893], dtype=int16),
  'pure_tone'),
 (array([-30000, -29900, -29800, ...,  10500,  10600,  10700], dtype=int16),
  'triangular_tone'),
 (array([30000, 30000, 30000, ..., 30000, 30000, 30000], dtype=int16),
  'square_tone')]
awf.get_wf_and_annots()
(array([  5183,  10421, -21645, ...,  30000,  30000,  30000], dtype=int16),
 {'random': [(0, 43008)],
  'pure_tone': [(43008, 86016)],
  'triangular_tone': [(86016, 129024)],
  'square_tone': [(129024, 172032)]})

Voiced time

Finally hum provides a function that will tell the time continuously with parameters for the frequency, speed, voice, volume, and time format

tell_time_continuously(every_secs=5, verbose=True)
15 45 11
15 45 16
15 45 21
15 45 26
KeyboardInterrupt!!!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hum-0.1.33.tar.gz (41.9 kB view details)

Uploaded Source

Built Distribution

hum-0.1.33-py3-none-any.whl (46.0 kB view details)

Uploaded Python 3

File details

Details for the file hum-0.1.33.tar.gz.

File metadata

  • Download URL: hum-0.1.33.tar.gz
  • Upload date:
  • Size: 41.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.15

File hashes

Hashes for hum-0.1.33.tar.gz
Algorithm Hash digest
SHA256 868106bd04d500a4febc0995a1e5296c344816839566af81bcfccdc22763fe6c
MD5 fb80dfc674e427778355a40af13122c4
BLAKE2b-256 b01b9ad601356a2e8c2f753995e47fbeb11759893b9ec7c4396fdbc7d9a0d421

See more details on using hashes here.

File details

Details for the file hum-0.1.33-py3-none-any.whl.

File metadata

  • Download URL: hum-0.1.33-py3-none-any.whl
  • Upload date:
  • Size: 46.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.15

File hashes

Hashes for hum-0.1.33-py3-none-any.whl
Algorithm Hash digest
SHA256 31126ed595f8e6a6a734cee940ec893a624a3c02af28ef455e2640b30d32a2e7
MD5 214ef6ead30540126b201b22d6904b12
BLAKE2b-256 7fd072157c418e027111ce0a77fa41fda94bb8de182ffb36ae0dac59af55583f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page