Skip to content
Snippets Groups Projects
Commit 62d44525 authored by Eric Kooistra's avatar Eric Kooistra
Browse files

Logging layout.

parent 7927866d
Branches
No related tags found
1 merge request!419Resolve RTSD-265
Pipeline #91851 passed
%% Cell type:markdown id:c69a2eb8 tags: %% Cell type:markdown id:c69a2eb8 tags:
# Try multirate by up and down sampling # Try multirate by up and down sampling
Author: Eric Kooistra, apr 2024 Author: Eric Kooistra, apr 2024
Purpose: Purpose:
* Practise DSP [1]. * Practise DSP [1].
References: References:
1. dsp_study_erko, summary of DSP books 1. dsp_study_erko, summary of DSP books
%% Cell type:code id:02689e50 tags: %% Cell type:code id:02689e50 tags:
``` python ``` python
import numpy as np import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from scipy import signal from scipy import signal
``` ```
%% Cell type:code id:acea4f0a tags: %% Cell type:code id:acea4f0a tags:
``` python ``` python
# Auto reload module when it is changed # Auto reload module when it is changed
%load_ext autoreload %load_ext autoreload
%autoreload 2 %autoreload 2
# Add rtdsp module path to $PYTHONPATH # Add rtdsp module path to $PYTHONPATH
import os import os
import sys import sys
module_path = os.path.abspath(os.path.join('../')) module_path = os.path.abspath(os.path.join('../'))
if module_path not in sys.path: if module_path not in sys.path:
sys.path.insert(0, module_path) sys.path.insert(0, module_path)
# Import rtdsp # Import rtdsp
from rtdsp.utilities import is_even from rtdsp.utilities import is_even
from rtdsp.firfilter import prototype_fir_low_pass_filter from rtdsp.firfilter import prototype_fir_low_pass_filter
from rtdsp.fourier import dtft from rtdsp.fourier import dtft
from rtdsp.multirate import downsample, upsample, resample from rtdsp.multirate import downsample, upsample, resample
from rtdsp.plotting import plot_power_spectrum from rtdsp.plotting import plot_power_spectrum
``` ```
%% Output
The autoreload extension is already loaded. To reload it, use:
%reload_ext autoreload
%% Cell type:code id:55e0fe37 tags: %% Cell type:code id:55e0fe37 tags:
``` python ``` python
# Samples # Samples
fs = 1 # sample rate fs = 1 # sample rate
ts = 1 / fs # sample period ts = 1 / fs # sample period
fNyquist = fs / 2 fNyquist = fs / 2
Nup = 3 Nup = 3
Ndown = 4 Ndown = 4
Nrate = np.max(np.array([Nup, Ndown])) Nrate = np.max(np.array([Nup, Ndown]))
``` ```
%% Cell type:markdown id:8be7a01e tags: %% Cell type:markdown id:8be7a01e tags:
# 1 Set up # 1 Set up
%% Cell type:markdown id:a949b9ff tags: %% Cell type:markdown id:a949b9ff tags:
## 1.1 Waveform generator (WG) ## 1.1 Waveform generator (WG)
%% Cell type:code id:e08b5ba2 tags: %% Cell type:code id:e08b5ba2 tags:
``` python ``` python
# Choose freq < fNyquist / Nrate # Choose freq < fNyquist / Nrate
freq = 0.3 * fNyquist / Nrate freq = 0.3 * fNyquist / Nrate
phase = np.pi / 3 phase = np.pi / 3
# Time # Time
Nperiods = 3 Nperiods = 3
period = 1 / freq period = 1 / freq
Nsim = int(Nperiods * period / ts) Nsim = int(Nperiods * period / ts)
print(str(Nsim)) print(str(Nsim))
tWg = np.arange(0, Nsim) * ts tWg = np.arange(0, Nsim) * ts
# Sinus signal # Sinus signal
wgData = np.sin(2 * np.pi * freq * tWg + phase) wgData = np.sin(2 * np.pi * freq * tWg + phase)
# Chirp signal # Chirp signal
fstart = freq * 1.0 fstart = freq * 1.0
fend = freq * 2.0 fend = freq * 2.0
tend = (Nsim - 1) * ts tend = (Nsim - 1) * ts
# wgData = signal.chirp(tWg, f0=fstart, f1=fend, t1=tend, method='linear', phi=phase) # wgData = signal.chirp(tWg, f0=fstart, f1=fend, t1=tend, method='linear', phi=phase)
plt.plot(tWg, wgData, 'r-', tWg, wgData, 'r*') plt.plot(tWg, wgData, 'r-', tWg, wgData, 'r*')
plt.title('WG data') plt.title('WG data')
plt.xlabel('t [ts = ' + str(ts) + ']') plt.xlabel('t [ts = ' + str(ts) + ']')
plt.ylabel('voltage') plt.ylabel('voltage')
plt.grid(True) plt.grid(True)
``` ```
%% Output %% Output
80 80
%% Cell type:markdown id:15bf6804 tags: %% Cell type:markdown id:15bf6804 tags:
## 1.2 Prototype FIR low pass filter ## 1.2 Prototype FIR low pass filter
%% Cell type:code id:0a69b385 tags: %% Cell type:code id:0a69b385 tags:
``` python ``` python
# Specifications # Specifications
Ntaps = 16 Ntaps = 16
Ncoefs = Nup * Ntaps Ncoefs = Nup * Ntaps
if is_even(Ncoefs): if is_even(Ncoefs):
Ncoefs -= 1 # make odd to have integer groupDelay Ncoefs -= 1 # make odd to have integer groupDelay
hpFactor = 0.9 hpFactor = 0.9
transitionFactor = 0.4 transitionFactor = 0.4
stopRippleFactor = 1000 stopRippleFactor = 1000
kaiserBeta = 1 kaiserBeta = 1
hPrototype = prototype_fir_low_pass_filter('firls', hPrototype = prototype_fir_low_pass_filter('firls',
Nup, Ntaps, Ncoefs, hpFactor, transitionFactor, stopRippleFactor, kaiserBeta) Nup, Ntaps, Ncoefs, hpFactor, transitionFactor, stopRippleFactor, kaiserBeta)
``` ```
%% Output %% Output
> design_fir_low_pass_filter(): > design_fir_low_pass_filter():
. Method = firls . Method = firls
. Q = 1.000000 . Q = 1.000000
. fpass = 0.150000 . fpass = 0.150000
. fstop = 0.210000 . fstop = 0.210000
. lpBW = 0.360000 . lpBW = 0.360000
. transistionBW = 0.060000 . transistionBW = 0.060000
. fNyquist = 0.500000 . fNyquist = 0.500000
. fs = 1.000000 . fs = 1.000000
. Ncoefs = 47 . Ncoefs = 47
. DC sum = 1.000000 . DC sum = 1.000000
. Symmetrical coefs = True . Symmetrical coefs = True
%% Cell type:code id:ca83f348 tags: %% Cell type:code id:ca83f348 tags:
``` python ``` python
# Plot impulse response # Plot impulse response
plt.figure(1) plt.figure(1)
plt.plot(hPrototype) plt.plot(hPrototype)
plt.title('Impulse response') plt.title('Impulse response')
plt.grid(True) plt.grid(True)
# Plot transfer function # Plot transfer function
plt.figure(2) plt.figure(2)
fLim = None # (-2, 2) fLim = None # (-2, 2)
dbLim = (-150, 5) dbLim = (-150, 5)
h, f, HF = dtft(hPrototype) h, f, HF = dtft(hPrototype)
plot_power_spectrum(f, HF, 'g', fs, fLim, dbLim) plot_power_spectrum(f, HF, 'g', fs, fLim, dbLim)
``` ```
%% Output %% Output
%% Cell type:markdown id:acca4f19 tags: %% Cell type:markdown id:acca4f19 tags:
# 2 Upsampling # 2 Upsampling
%% Cell type:code id:5a402a30 tags: %% Cell type:code id:5a402a30 tags:
``` python ``` python
# Adjust output time axis for group delay to align output with input in plot # Adjust output time axis for group delay to align output with input in plot
groupDelay = (Ncoefs - 1) / 2 groupDelay = (Ncoefs - 1) / 2
print('Ncoefs = ' + str(len(hPrototype))) print('Ncoefs = ' + str(len(hPrototype)))
print('groupDelay = ' + str(groupDelay / Nup) + ' input samples') print('groupDelay = ' + str(groupDelay / Nup) + ' input samples')
print('groupDelay = ' + str(groupDelay) + ' interpolated samples') print('groupDelay = ' + str(groupDelay) + ' interpolated samples')
tsUp = ts / Nup tsUp = ts / Nup
tUp = (np.arange(0, Nsim * Nup) - groupDelay) * tsUp tUp = (np.arange(0, Nsim * Nup) - groupDelay) * tsUp
coefs = hPrototype * Nup coefs = hPrototype * Nup
upsampledData = upsample(wgData, Nup, coefs, verify=True) upsampledData = upsample(wgData, Nup, coefs, verify=True)
``` ```
%% Output %% Output
Ncoefs = 47 Ncoefs = 47
groupDelay = 7.666666666666667 input samples groupDelay = 7.666666666666667 input samples
groupDelay = 23.0 interpolated samples groupDelay = 23.0 interpolated samples
> Verify upsample(): > Verify upsample():
. PASSED: correct upsample result . PASSED: correct upsample result
> Log upsample(): > Log upsample():
. Nup = 3 . Nup = 3
. Nx = 80 . Nx = 80
. len(y) = 240 . len(y) = 240
%% Cell type:code id:ee8666c6 tags: %% Cell type:code id:ee8666c6 tags:
``` python ``` python
# First plot up, then wg, to show wg samples as well # First plot up, then wg, to show wg samples as well
plt.plot(tUp, upsampledData, 'b-', tUp, upsampledData, 'b*') plt.plot(tUp, upsampledData, 'b-', tUp, upsampledData, 'b*')
plt.plot(tWg, wgData, 'r-', tWg, wgData, 'r*') plt.plot(tWg, wgData, 'r-', tWg, wgData, 'r*')
plt.title('Upsampled data (Nup = %d)' % Nup) plt.title('Upsampled data (Nup = %d)' % Nup)
plt.xlabel('t [ts = ' + str(ts) + ']') plt.xlabel('t [ts = ' + str(ts) + ']')
plt.ylabel('voltage') plt.ylabel('voltage')
plt.xlim([18, 22]) plt.xlim([18, 22])
plt.legend(['up', '', 'wg', '']) plt.legend(['up', '', 'wg', ''])
plt.grid(True) plt.grid(True)
``` ```
%% Output %% Output
%% Cell type:markdown id:854dbd8d tags: %% Cell type:markdown id:854dbd8d tags:
# 3 Downsampling # 3 Downsampling
%% Cell type:code id:860828a7 tags: %% Cell type:code id:860828a7 tags:
``` python ``` python
# Adjust output time axis for group delay to align output with input in plot # Adjust output time axis for group delay to align output with input in plot
groupDelay = (Ncoefs - 1) / 2 groupDelay = (Ncoefs - 1) / 2
print('Ncoefs = ' + str(len(hPrototype))) print('Ncoefs = ' + str(len(hPrototype)))
print('groupDelay = ' + str(groupDelay) + ' input samples') print('groupDelay = ' + str(groupDelay) + ' input samples')
print('groupDelay = ' + str(groupDelay / Ndown) + ' downsampled samples') print('groupDelay = ' + str(groupDelay / Ndown) + ' downsampled samples')
tsDown = ts * Ndown tsDown = ts * Ndown
Dsim = 1 + (Nsim - 1) // Ndown Dsim = 1 + (Nsim - 1) // Ndown
tDown = (np.arange(0, Dsim) * tsDown) - groupDelay * ts tDown = (np.arange(0, Dsim) * tsDown) - groupDelay * ts
coefs = hPrototype coefs = hPrototype
downsampledData = downsample(wgData, Ndown, coefs, verify=True) downsampledData = downsample(wgData, Ndown, coefs, verify=True)
``` ```
%% Output %% Output
Ncoefs = 47 Ncoefs = 47
groupDelay = 23.0 input samples groupDelay = 23.0 input samples
groupDelay = 5.75 downsampled samples groupDelay = 5.75 downsampled samples
> Verify downsample(): > Verify downsample():
. PASSED: correct downsample result . PASSED: correct downsample result
> Log downsample(): > Log downsample():
. len(x) = 80 . len(x) = 80
. Ndown = 4 . Ndown = 4
. Nx = 77 . Nx = 77
. Nxp = 20 . Nxp = 20
. len(y) = 20 . len(y) = 20
%% Cell type:code id:53d6535f tags: %% Cell type:code id:53d6535f tags:
``` python ``` python
plt.plot(tWg, wgData, 'r-', tWg, wgData, 'r*') plt.plot(tWg, wgData, 'r-', tWg, wgData, 'r*')
plt.plot(tDown, downsampledData, 'b-', tDown, downsampledData, 'b*') plt.plot(tDown, downsampledData, 'b-', tDown, downsampledData, 'b*')
plt.title('Downsampled data (Ndown = %d)' % Ndown) plt.title('Downsampled data (Ndown = %d)' % Ndown)
plt.xlabel('t [ts = ' + str(ts) + ']') plt.xlabel('t [ts = ' + str(ts) + ']')
plt.ylabel('voltage') plt.ylabel('voltage')
#plt.xlim([55, 65]) #plt.xlim([55, 65])
plt.legend(['wg', '', 'down', '']) plt.legend(['wg', '', 'down', ''])
plt.grid(True) plt.grid(True)
``` ```
%% Output %% Output
%% Cell type:markdown id:b13c9442 tags: %% Cell type:markdown id:b13c9442 tags:
# 4 Resampling # 4 Resampling
%% Cell type:code id:ed8a87a4 tags: %% Cell type:code id:ed8a87a4 tags:
``` python ``` python
# Adjust output time axis for group delay to align output with input in plot # Adjust output time axis for group delay to align output with input in plot
groupDelay = (Ncoefs - 1) / 2 groupDelay = (Ncoefs - 1) / 2
print('Ncoefs = ' + str(len(hPrototype))) print('Ncoefs = ' + str(len(hPrototype)))
print('groupDelay = ' + str(groupDelay / Nup) + ' input samples') print('groupDelay = ' + str(groupDelay / Nup) + ' input samples')
print('groupDelay = ' + str(groupDelay) + ' interpolated samples') print('groupDelay = ' + str(groupDelay) + ' interpolated samples')
print('groupDelay = ' + str(groupDelay / Ndown) + ' resampled samples') print('groupDelay = ' + str(groupDelay / Ndown) + ' resampled samples')
tsUp = ts / Nup tsUp = ts / Nup
tsDown = tsUp * Ndown tsDown = tsUp * Ndown
Dsim = 1 + (Nsim * Nup - 1) // Ndown Dsim = 1 + (Nsim * Nup - 1) // Ndown
tResample = (np.arange(0, Dsim) * tsDown) - groupDelay * tsUp tResample = (np.arange(0, Dsim) * tsDown) - groupDelay * tsUp
coefs = hPrototype * Nup coefs = hPrototype * Nup
resampledData = resample(wgData, Nup, Ndown, coefs, verify=True) resampledData = resample(wgData, Nup, Ndown, coefs, verify=True)
``` ```
%% Output %% Output
Ncoefs = 47 Ncoefs = 47
groupDelay = 7.666666666666667 input samples groupDelay = 7.666666666666667 input samples
groupDelay = 23.0 interpolated samples groupDelay = 23.0 interpolated samples
groupDelay = 5.75 resampled samples groupDelay = 5.75 resampled samples
> Log upsample(): > Log upsample():
. Nup = 3 . Nup = 3
. Nx = 80 . Nx = 80
. len(y) = 240 . len(y) = 240
> Verify resample(): > Verify resample():
. PASSED: correct resample result . PASSED: correct resample result
> Log resample(): > Log resample():
. len(x) = 80 . len(x) = 80
. Nx = 80 . Nx = 80
. len(v) = 240 . len(v) = 240
. Ny = 237 . Ny = 237
. Nyp = 60 . Nyp = 60
. len(y) = 60 . len(y) = 60
%% Cell type:code id:d085cf51 tags: %% Cell type:code id:d085cf51 tags:
``` python ``` python
plt.plot(tWg, wgData, 'r-', tWg, wgData, 'r*') plt.plot(tWg, wgData, 'r-', tWg, wgData, 'r*')
plt.plot(tResample, resampledData, 'b-', tResample, resampledData, 'b*') plt.plot(tResample, resampledData, 'b-', tResample, resampledData, 'b*')
plt.title('Resampled data (Nup / Ndown = %d / %d)' % (Nup, Ndown)) plt.title('Resampled data (Nup / Ndown = %d / %d)' % (Nup, Ndown))
plt.xlabel('t [ts = ' + str(ts) + ']') plt.xlabel('t [ts = ' + str(ts) + ']')
plt.ylabel('voltage') plt.ylabel('voltage')
#plt.xlim([55, 65]) #plt.xlim([55, 65])
plt.xlim([-5, 15]) plt.xlim([-5, 15])
plt.xlim([-5, 25]) plt.xlim([-5, 25])
plt.legend(['wg', '', 'resampled', '']) plt.legend(['wg', '', 'resampled', ''])
plt.grid(True) plt.grid(True)
``` ```
%% Output %% Output
%% Cell type:code id:b4a95b55 tags: %% Cell type:code id:b4a95b55 tags:
``` python ``` python
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment