Quickstart guide to pyPCG

Import pyPCG and some utilities

import pyPCG as pcg
import pyPCG.io as pcg_io

import matplotlib.pyplot as plt
%matplotlib widget

Read in data

Print length in samples and samplerate

For supported formats, see pyPCG.io.read_signal_file documentation

data, fs = pcg_io.read_signal_file("example.wav","wav")
print(len(data),fs)
19980 333

Create a signal object

Print it to see information about the signal

signal = pcg.pcg_signal(data,fs)
print(signal)
PCG signal [60.0s 333Hz] ['File read in']

Another option, by using the * operator:

signal = pcg.pcg_signal(*pcg_io.read_signal_file("example.wav","wav"))
print(signal)
PCG signal [60.0s 333Hz] ['File read in']

Plot the signal

Limit the plot to the first two seconds The title by default is the last operation done on the signal

Note, that the center value is not 0, and the range is not [-1,1]. This is because the values were stored in uint8 format.

plt.figure()
pcg.plot(signal)
plt.xlim((0,2))
(0.0, 2.0)

Normalize the signal

Rescale the signal to [-1,1] range and center it to zero.

To better illustrate this, use zeroline=True while plotting.

center_signal = pcg.zero_center(signal)
scale_signal = pcg.unit_scale(center_signal)

plt.figure()
pcg.plot(scale_signal,zeroline=True)
plt.xlim((0,2))

print(scale_signal)
PCG signal [60.0s 333Hz] ['File read in', 'Zero center', 'Unit scale']

Since this combination of operations is quite common, the normalize function combines zero_center and unit_scale.

norm_signal = pcg.normalize(signal)

plt.figure()
pcg.plot(norm_signal,zeroline=True)
plt.xlim((0,2))

print(norm_signal)
PCG signal [60.0s 333Hz] ['File read in', 'Zero center', 'Unit scale']