1
0
Fork 0
cp/usth/ICT2.9/practical/dsp.ipynb

32 KiB

DSP Labwork

Given the following signals:

In [ ]:
input_1kHz_15kHz = [
    +0.0000000000, +0.5924659585, -0.0947343455, +0.1913417162, +1.0000000000,
    +0.4174197128, +0.3535533906, +1.2552931065, +0.8660254038, +0.4619397663,
    +1.3194792169, +1.1827865776, +0.5000000000, +1.1827865776, +1.3194792169,
    +0.4619397663, +0.8660254038, +1.2552931065, +0.3535533906, +0.4174197128,
    +1.0000000000, +0.1913417162, -0.0947343455, +0.5924659585, -0.0000000000,
    -0.5924659585, +0.0947343455, -0.1913417162, -1.0000000000, -0.4174197128,
    -0.3535533906, -1.2552931065, -0.8660254038, -0.4619397663, -1.3194792169,
    -1.1827865776, -0.5000000000, -1.1827865776, -1.3194792169, -0.4619397663,
    -0.8660254038, -1.2552931065, -0.3535533906, -0.4174197128, -1.0000000000,
    -0.1913417162, +0.0947343455, -0.5924659585, +0.0000000000, +0.5924659585,
    -0.0947343455, +0.1913417162, +1.0000000000, +0.4174197128, +0.3535533906,
    +1.2552931065, +0.8660254038, +0.4619397663, +1.3194792169, +1.1827865776,
    +0.5000000000, +1.1827865776, +1.3194792169, +0.4619397663, +0.8660254038,
    +1.2552931065, +0.3535533906, +0.4174197128, +1.0000000000, +0.1913417162,
    -0.0947343455, +0.5924659585, +0.0000000000, -0.5924659585, +0.0947343455,
    -0.1913417162, -1.0000000000, -0.4174197128, -0.3535533906, -1.2552931065,
    -0.8660254038, -0.4619397663, -1.3194792169, -1.1827865776, -0.5000000000,
    -1.1827865776, -1.3194792169, -0.4619397663, -0.8660254038, -1.2552931065,
    -0.3535533906, -0.4174197128, -1.0000000000, -0.1913417162, +0.0947343455,
    -0.5924659585, +0.0000000000, +0.5924659585, -0.0947343455, +0.1913417162,
    +1.0000000000, +0.4174197128, +0.3535533906, +1.2552931065, +0.8660254038,
    +0.4619397663, +1.3194792169, +1.1827865776, +0.5000000000, +1.1827865776,
    +1.3194792169, +0.4619397663, +0.8660254038, +1.2552931065, +0.3535533906,
    +0.4174197128, +1.0000000000, +0.1913417162, -0.0947343455, +0.5924659585,
    +0.0000000000, -0.5924659585, +0.0947343455, -0.1913417162, -1.0000000000,
    -0.4174197128, -0.3535533906, -1.2552931065, -0.8660254038, -0.4619397663,
    -1.3194792169, -1.1827865776, -0.5000000000, -1.1827865776, -1.3194792169,
    -0.4619397663, -0.8660254038, -1.2552931065, -0.3535533906, -0.4174197128,
    -1.0000000000, -0.1913417162, +0.0947343455, -0.5924659585, -0.0000000000,
    +0.5924659585, -0.0947343455, +0.1913417162, +1.0000000000, +0.4174197128,
    +0.3535533906, +1.2552931065, +0.8660254038, +0.4619397663, +1.3194792169,
    +1.1827865776, +0.5000000000, +1.1827865776, +1.3194792169, +0.4619397663,
    +0.8660254038, +1.2552931065, +0.3535533906, +0.4174197128, +1.0000000000,
    +0.1913417162, -0.0947343455, +0.5924659585, -0.0000000000, -0.5924659585,
    +0.0947343455, -0.1913417162, -1.0000000000, -0.4174197128, -0.3535533906,
    -1.2552931065, -0.8660254038, -0.4619397663, -1.3194792169, -1.1827865776,
    -0.5000000000, -1.1827865776, -1.3194792169, -0.4619397663, -0.8660254038,
    -1.2552931065, -0.3535533906, -0.4174197128, -1.0000000000, -0.1913417162,
    +0.0947343455, -0.5924659585, +0.0000000000, +0.5924659585, -0.0947343455,
    +0.1913417162, +1.0000000000, +0.4174197128, +0.3535533906, +1.2552931065,
    +0.8660254038, +0.4619397663, +1.3194792169, +1.1827865776, +0.5000000000,
    +1.1827865776, +1.3194792169, +0.4619397663, +0.8660254038, +1.2552931065,
    +0.3535533906, +0.4174197128, +1.0000000000, +0.1913417162, -0.0947343455,
    +0.5924659585, +0.0000000000, -0.5924659585, +0.0947343455, -0.1913417162,
    -1.0000000000, -0.4174197128, -0.3535533906, -1.2552931065, -0.8660254038,
    -0.4619397663, -1.3194792169, -1.1827865776, -0.5000000000, -1.1827865776,
    -1.3194792169, -0.4619397663, -0.8660254038, -1.2552931065, -0.3535533906,
    -0.4174197128, -1.0000000000, -0.1913417162, +0.0947343455, -0.5924659585,
    -0.0000000000, +0.5924659585, -0.0947343455, +0.1913417162, +1.0000000000,
    +0.4174197128, +0.3535533906, +1.2552931065, +0.8660254038, +0.4619397663,
    +1.3194792169, +1.1827865776, +0.5000000000, +1.1827865776, +1.3194792169,
    +0.4619397663, +0.8660254038, +1.2552931065, +0.3535533906, +0.4174197128,
    +1.0000000000, +0.1913417162, -0.0947343455, +0.5924659585, +0.0000000000,
    -0.5924659585, +0.0947343455, -0.1913417162, -1.0000000000, -0.4174197128,
    -0.3535533906, -1.2552931065, -0.8660254038, -0.4619397663, -1.3194792169,
    -1.1827865776, -0.5000000000, -1.1827865776, -1.3194792169, -0.4619397663,
    -0.8660254038, -1.2552931065, -0.3535533906, -0.4174197128, -1.0000000000,
    -0.1913417162, +0.0947343455, -0.5924659585, -0.0000000000, +0.5924659585,
    -0.0947343455, +0.1913417162, +1.0000000000, +0.4174197128, +0.3535533906,
    +1.2552931065, +0.8660254038, +0.4619397663, +1.3194792169, +1.1827865776,
    +0.5000000000, +1.1827865776, +1.3194792169, +0.4619397663, +0.8660254038,
    +1.2552931065, +0.3535533906, +0.4174197128, +1.0000000000, +0.1913417162,
    -0.0947343455, +0.5924659585, +0.0000000000, -0.5924659585, +0.0947343455,
    -0.1913417162, -1.0000000000, -0.4174197128, -0.3535533906, -1.2552931065]
impulse_response = [
    -0.0018225230, -0.0015879294, +0.0000000000, +0.0036977508, +0.0080754303,
    +0.0085302217, -0.0000000000, -0.0173976984, -0.0341458607, -0.0333591565,
    +0.0000000000, +0.0676308395, +0.1522061835, +0.2229246956, +0.2504960933,
    +0.2229246956, +0.1522061835, +0.0676308395, +0.0000000000, -0.0333591565,
    -0.0341458607, -0.0173976984, -0.0000000000, +0.0085302217, +0.0080754303,
    +0.0036977508, +0.0000000000, -0.0015879294, -0.0018225230]

Signals

Then the input signal can be plotted as following (the x-axis is wrong of course, we will tackle this later on). At first glance, it might be obvious (not for me though) that the input is a superposition of two sine waves.

In [ ]:
import matplotlib.pyplot as plt


def quickplt(sequence):
    """Plot the signal as-is."""
    plt.plot(sequence)
    plt.show()


quickplt(input_1kHz_15kHz)

To confirm this suspection, we apply FFT on the signals and plot the results:

In [ ]:
from math import pi

import numpy as np
from numpy.fft import fft


def plt_polar(sequence):
    """Plot the complex signal in polar coordinate, from 0 to pi*2."""
    fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
    domain = np.linspace(0, pi*2, len(sequence))
    ax1.plot(domain, np.abs(sequence))
    ax1.set_title('magnitude')
    ax2.plot(domain, np.angle(sequence))
    ax2.set_title('phase')
    plt.show()


inpft = fft(input_1kHz_15kHz)
plt_polar(inpft)

Since the low frequencies are lurking around k*pi*2 and the high frequencies are around k*pi*2 + pi, we can make a good guess that the higher peak is of 1 kHz and the lower one is of 15 kHz. The sample rate would be at exactlyk*pi*2 + pi and can be calculated as

In [ ]:
sample_rate = (len(inpft)/2) / np.argmax(inpft) * 1000
print(sample_rate)

We can then replot the signal with the correct scaling

In [ ]:
def plt_time(sequence):
    """Plot the signal in time domain."""
    length = len(sequence)
    plt.plot(np.linspace(0, length/sample_rate, length), sequence)
    plt.show()


plt_time(input_1kHz_15kHz)

The plot of these in cartesian coordinates doesn't give me any further understanding, however:

In [ ]:
def plt_rect(sequence):
    """Plot the complex signal in rectangular coordinate, from 0 to pi*2.""" 
    fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
    domain = np.linspace(0, pi*2, len(sequence))
    ax1.plot(domain, np.real(sequence))
    ax1.set_title('real')
    ax2.plot(domain, np.imag(sequence))
    ax2.set_title('imaginary')
    plt.show()


plt_rect(inpft)

Systems

Low-pass Filter

In this section, we also try to do the same thing for the impulse response, which seems to be a sinc function.

In [ ]:
quickplt(impulse_response)
lfft = fft(impulse_response)
plt_polar(lfft)
plt_rect(lfft)

As shown in the graphs, the system is a low-pass filter.

Applying the system to the input we get what is undeniably the sinusoidal signal of frequency of 1 kHz:

In [ ]:
output = np.convolve(input_1kHz_15kHz, impulse_response)
plt_time(output)

Alternative to convolution in time domain, we can multiply the FTs:

In [ ]:
from numpy.fft import ifft
from scipy import interpolate

f = interpolate.interp1d(np.linspace(0, pi*2, len(lfft)), lfft, kind='zero')
plt_time(np.real(ifft(inpft*f(np.linspace(0, pi*2, len(inpft))))))

It is noticeable that the wave is now distorted in shape. Funny enough, using other interpolation methods, the result is much worse (using the convoluted one as the reference), for example the linear one:

In [ ]:
f = interpolate.interp1d(np.linspace(0, pi*2, len(lfft)), lfft, kind='linear')
plt_time(np.real(ifft(inpft*f(np.linspace(0, pi*2, len(inpft))))))

Notice that the low-pass filter filtered out the 15 kHz wave:

In [ ]:
plt_polar(fft(output))
plt_rect(fft(output))

High-pass filter

To turn the given low-pass filter to a high-pass one, we subtract it from the impulse signal (which is equivalent to subtracting it from 1 in the frequency domain thanks to linearity):

In [ ]:
high_pass = ((lambda m: [0]*m + [1] + [0]*m)(np.argmax(impulse_response))
             - np.array(impulse_response))
quickplt(high_pass)
plt_polar(fft(high_pass))
plt_rect(fft(high_pass))

We then apply it to the input and get the high frequency signal of 15 kHz:

In [ ]:
outputhf = np.convolve(input_1kHz_15kHz, high_pass)
plt_time(outputhf)
plt_polar(fft(outputhf))
plt_rect(fft(outputhf))

Other methods to create a HF filter have been tried, however the result is nowhere as good:

  1. Shifting the low-pass filter by pi in frequency domain:
In [ ]:
high_pass_bad = ifft(np.roll(lfft, len(impulse_response)>>1))
outputhf_bad = np.convolve(input_1kHz_15kHz, high_pass_bad)
plt_rect(outputhf_bad)
plt_polar(fft(outputhf_bad))
  1. Multiply the low-pass in time domain with (-1)^n (which effectively also shift it by pi):
In [ ]:
high_pass_worse = np.fromiter(((-1)**n for n in range(len(impulse_response))),
                              dtype=float) * impulse_response
outputhf_worse = np.convolve(input_1kHz_15kHz, high_pass_worse)
plt_time(outputhf_worse)
plt_polar(fft(outputhf_worse))

While both of these produce a high frequency signal for most of the interval, at the start and end the volume is significantly higher and there are many different frequencies instead of just 15 kHz. This seems disagrees with the theory at first, but the theory is only supposed to apply to infinite length impulse response; manipulate a rather small in size sample cannot give perfect results.

Electrocardiogram

Given the following ECG signal:

In [ ]:
ecg = [
    0, 0.0010593, 0.0021186, 0.003178, 0.0042373, 0.0052966, 0.0063559,
    0.0074153, 0.0084746, 0.045198, 0.081921, 0.11864, 0.15537, 0.19209,
    0.22881, 0.26554, 0.30226, 0.33898, 0.30226, 0.26554, 0.22881, 0.19209,
    0.15537, 0.11864, 0.081921, 0.045198, 0.0084746, 0.0077684, 0.0070621,
    0.0063559, 0.0056497, 0.0049435, 0.0042373, 0.0035311, 0.0028249,
    0.0021186, 0.0014124, 0.00070621, 0, -0.096045, -0.19209, -0.28814,
    -0.073446, 0.14124, 0.35593, 0.57062, 0.78531, 1, 0.73729, 0.47458,
    0.21186, -0.050847, -0.31356, -0.57627, -0.83898, -0.55932, -0.27966, 0,
    0.00073692, 0.0014738, 0.0022108, 0.0029477, 0.0036846, 0.0044215,
    0.0051584, 0.0058954, 0.0066323, 0.0073692, 0.0081061, 0.008843, 0.00958,
    0.010317, 0.011054, 0.011791, 0.012528, 0.013265, 0.014001, 0.014738,
    0.015475, 0.016212, 0.016949, 0.03484, 0.052731, 0.070621, 0.088512,
    0.1064, 0.12429, 0.14218, 0.16008, 0.17797, 0.16186, 0.14576, 0.12966,
    0.11356, 0.097458, 0.081356, 0.065254, 0.049153, 0.033051, 0.016949,
    0.013559, 0.010169, 0.0067797, 0.0033898, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0010593, 0.0021186, 0.003178,
    0.0042373, 0.0052966, 0.0063559, 0.0074153, 0.0084746, 0.045198, 0.081921,
    0.11864, 0.15537, 0.19209, 0.22881, 0.26554, 0.30226, 0.33898, 0.30226,
    0.26554, 0.22881, 0.19209, 0.15537, 0.11864, 0.081921, 0.045198, 0.0084746,
    0.0077684, 0.0070621, 0.0063559, 0.0056497, 0.0049435, 0.0042373,
    0.0035311, 0.0028249, 0.0021186, 0.0014124, 0.00070621, 0, -0.096045,
    -0.19209, -0.28814, -0.073446, 0.14124, 0.35593, 0.57062, 0.78531, 1,
    0.73729, 0.47458, 0.21186, -0.050847, -0.31356, -0.57627, -0.83898,
    -0.55932, -0.27966, 0, 0.00073692, 0.0014738, 0.0022108, 0.0029477,
    0.0036846, 0.0044215, 0.0051584, 0.0058954, 0.0066323, 0.0073692,
    0.0081061, 0.008843, 0.00958, 0.010317, 0.011054, 0.011791, 0.012528,
    0.013265, 0.014001, 0.014738, 0.015475, 0.016212, 0.016949, 0.03484,
    0.052731, 0.070621, 0.088512, 0.1064, 0.12429, 0.14218, 0.16008, 0.17797,
    0.16186, 0.14576, 0.12966, 0.11356, 0.097458, 0.081356, 0.065254, 0.049153,
    0.033051, 0.016949, 0.013559, 0.010169, 0.0067797, 0.0033898, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0010593,
    0.0021186, 0.003178, 0.0042373, 0.0052966, 0.0063559, 0.0074153, 0.0084746,
    0.045198, 0.081921, 0.11864, 0.15537, 0.19209, 0.22881, 0.26554, 0.30226,
    0.33898, 0.30226, 0.26554, 0.22881, 0.19209, 0.15537, 0.11864, 0.081921,
    0.045198, 0.0084746, 0.0077684, 0.0070621, 0.0063559, 0.0056497, 0.0049435,
    0.0042373, 0.0035311, 0.0028249, 0.0021186, 0.0014124, 0.00070621, 0,
    -0.096045, -0.19209, -0.28814, -0.073446, 0.14124, 0.35593, 0.57062,
    0.78531, 1, 0.73729, 0.47458, 0.21186, -0.050847, -0.31356, -0.57627,
    -0.83898, -0.55932, -0.27966, 0, 0.00073692, 0.0014738, 0.0022108,
    0.0029477, 0.0036846, 0.0044215, 0.0051584, 0.0058954, 0.0066323,
    0.0073692, 0.0081061, 0.008843, 0.00958, 0.010317, 0.011054, 0.011791,
    0.012528, 0.013265, 0.014001, 0.014738, 0.015475, 0.016212, 0.016949,
    0.03484, 0.052731, 0.070621, 0.088512, 0.1064, 0.12429, 0.14218, 0.16008,
    0.17797, 0.16186, 0.14576, 0.12966, 0.11356, 0.097458, 0.081356, 0.065254,
    0.049153, 0.033051, 0.016949, 0.013559, 0.010169, 0.0067797, 0.0033898, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0.0010593, 0.0021186, 0.003178, 0.0042373, 0.0052966, 0.0063559, 0.0074153,
    0.0084746, 0.045198, 0.081921, 0.11864, 0.15537, 0.19209, 0.22881, 0.26554,
    0.30226, 0.33898, 0.30226, 0.26554, 0.22881, 0.19209, 0.15537, 0.11864,
    0.081921, 0.045198, 0.0084746, 0.0077684, 0.0070621, 0.0063559, 0.0056497,
    0.0049435, 0.0042373, 0.0035311, 0.0028249, 0.0021186, 0.0014124,
    0.00070621, 0, -0.096045, -0.19209, -0.28814, -0.073446, 0.14124, 0.35593,
    0.57062, 0.78531, 1, 0.73729, 0.47458, 0.21186, -0.050847, -0.31356,
    -0.57627, -0.83898, -0.55932, -0.27966, 0, 0.00073692, 0.0014738,
    0.0022108, 0.0029477, 0.0036846, 0.0044215, 0.0051584, 0.0058954,
    0.0066323, 0.0073692, 0.0081061, 0.008843, 0.00958, 0.010317, 0.011054,
    0.011791, 0.012528, 0.013265, 0.014001, 0.014738, 0.015475, 0.016212,
    0.016949, 0.03484, 0.052731, 0.070621, 0.088512, 0.1064, 0.12429, 0.14218,
    0.16008, 0.17797, 0.16186, 0.14576, 0.12966, 0.11356, 0.097458, 0.081356,
    0.065254, 0.049153, 0.033051, 0.016949, 0.013559, 0.010169, 0.0067797,
    0.0033898, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0.0010593, 0.0021186, 0.003178, 0.0042373, 0.0052966,
    0.0063559, 0.0074153, 0.0084746, 0.045198, 0.081921, 0.11864, 0.15537,
    0.19209, 0.22881, 0.26554, 0.30226, 0.33898, 0.30226, 0.26554, 0.22881,
    0.19209, 0.15537, 0.11864, 0.081921, 0.045198, 0.0084746, 0.0077684,
    0.0070621, 0.0063559, 0.0056497, 0.0049435, 0.0042373, 0.0035311,
    0.0028249, 0.0021186, 0.0014124, 0.00070621, 0, -0.096045, -0.19209,
    -0.28814, -0.073446, 0.14124, 0.35593, 0.57062, 0.78531, 1, 0.73729,
    0.47458, 0.21186, -0.050847, -0.31356, -0.57627, -0.83898, -0.55932,
    -0.27966, 0, 0.00073692, 0.0014738, 0.0022108, 0.0029477, 0.0036846,
    0.0044215, 0.0051584, 0.0058954, 0.0066323, 0.0073692, 0.0081061, 0.008843,
    0.00958, 0.010317, 0.011054, 0.011791, 0.012528, 0.013265, 0.014001,
    0.014738, 0.015475, 0.016212, 0.016949, 0.03484, 0.052731, 0.070621,
    0.088512, 0.1064, 0.12429, 0.14218, 0.16008, 0.17797, 0.16186, 0.14576,
    0.12966, 0.11356, 0.097458, 0.081356, 0.065254, 0.049153, 0.033051,
    0.016949, 0.013559, 0.010169, 0.0067797, 0.0033898, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
quickplt(ecg)
plt_polar(fft(ecg))
plt_rect(fft(ecg))

The plots of the ECG gives us the initial intuition that it's a rather low frequency signal (in fact we have the heartrate of somewhere between 50 to 500 Hz) with multiple face each priod. This is confirmed by the low-passed response, which looks surprisingly similar to the original in time domain

In [ ]:
ecglf = np.convolve(ecg, impulse_response)
quickplt(ecglf)
plt_polar(fft(ecglf))
plt_rect(fft(ecglf))

Just for fun, we add some high frequency noise to the signal (because heartbeats are repetitive and boring!):

In [ ]:
real, imag = np.random.random((2, len(ecg)-len(high_pass)+1))
whitenoise = ifft(real + imag*1j)
noise_hf = abs(np.convolve(whitenoise, high_pass))
quickplt(noise_hf)
noisy_ecg = ecg + noise_hf
quickplt(noisy_ecg)

To smoothen the noisy signal back to normal, the low pass filter shoud be able to does the job:

In [ ]:
recovered_ecg = np.convolve(noisy_ecg, impulse_response)
quickplt(recovered_ecg)

Bonus

In this section, we'll try to have some fun playing the signals using palace. While the main purpose of palace is positional audio rendering and environmental effect, it also provide a handy decoder base class, which can be easily derived:

In [ ]:
!pip install palace
from palace import BaseDecoder, Buffer, Context, Device


class Dec(BaseDecoder):
    """Generator of elementary signals."""
    def __init__(self, content):
        self.content, self.size = content.copy(), len(content)

    @BaseDecoder.frequency.getter
    def frequency(self) -> int: return int(sample_rate)

    @BaseDecoder.channel_config.getter
    def channel_config(self) -> str:
        return 'Mono'

    @BaseDecoder.sample_type.getter
    def sample_type(self) -> str:
        return '32-bit float'

    @BaseDecoder.length.getter
    def length(self) -> int: return self.size

    def seek(self, pos: int) -> bool: return False

    @BaseDecoder.loop_points.getter
    def loop_points(self): return 0, 0

    def read(self, count: int) -> bytes:
        if count > len(self.content):
            try:
                return np.float32(self.content).tobytes()
            finally:
                self.content = []
        data, self.content = self.content[:count], self.content[count:]
        return np.float32(data).tobytes()

The input and output signals can then be played by running:

In [ ]:
from time import sleep

with Device() as d, Context(d) as c:
    with Buffer.from_decoder(Dec(input_1kHz_15kHz), 'input') as b, b.play() as s:
        sleep(1)
    with Buffer.from_decoder(Dec(output), 'lf') as b, b.play() as s:
        sleep(1)
    with Buffer.from_decoder(Dec(outputhf), 'hf') as b, b.play() as s:
        sleep(1)