Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
H
HDL
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
RTSD
HDL
Commits
42eb53a3
Commit
42eb53a3
authored
1 year ago
by
Eric Kooistra
Browse files
Options
Downloads
Patches
Plain Diff
Support show argument in plot_iir_filter_analysis().
parent
1277a47c
No related branches found
No related tags found
1 merge request
!374
Add plot_iir_filter_analysis() based on LTF-IIR-allgemein.ipynbcode from...
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
applications/lofar2/model/pfb_os/dsp.py
+43
-19
43 additions, 19 deletions
applications/lofar2/model/pfb_os/dsp.py
with
43 additions
and
19 deletions
applications/lofar2/model/pfb_os/dsp.py
+
43
−
19
View file @
42eb53a3
...
...
@@ -339,19 +339,29 @@ def plot_time_response(h, name='', markers=False):
plt
.
grid
(
True
)
def
plot_iir_filter_analysis
(
b
,
a
,
fs
=
1
,
Ntime
=
100
,
step
=
False
):
"""
Plot iir filter analysis results.
def
plot_iir_filter_analysis
(
b
,
a
,
fs
=
1
,
whole
=
False
,
Ntime
=
100
,
step
=
False
,
show
=
[]
):
"""
Plot
and print
iir filter analysis results.
Input:
. b, a: IIR filter coefficients in same format as for scipy.signal.freqz
. fs: sample frequency
. whole: False to have only positive frequencies, so 0 to fs / 2 in
spectrum plots, True to have 0 to fs.
. Ntime: number of timesamples for impulse response
. step: False for impulse response, True for step response
. show[]: which plots to show from:
'
zplane
'
,
'
power spectrum
'
,
'
phase spectrum
'
,
'
time response
'
Return: z, p, k
"""
# Plot poles / zeros diagram in z-plane
if
'
zplane
'
in
show
:
fig1
,
ax1
=
plt
.
subplots
(
1
)
ax1
.
set_title
(
'
Zeros (o) and poles (x) in z-plane
'
)
z
,
p
,
k
=
dsp_fpga_lib
.
zplane
(
b
,
a
,
plt_ax
=
ax1
)
# uses np.roots(a), np.roots(b)
else
:
z
,
p
,
k
=
dsp_fpga_lib
.
zplane
(
b
,
a
)
# no plot, only calculate z, p, k
print
(
'
Zeros, poles and gain from b, a coefficients:
'
)
if
len
(
z
)
>
0
:
print
(
'
. zeros:
'
)
...
...
@@ -370,24 +380,38 @@ def plot_iir_filter_analysis(b, a, fs=1, Ntime=100, step=False):
# Plot transfer function H(f), is H(z) for z = exp(j w), so along the unit circle
# . 0 Hz at 1 + 0j, fs / 4 at 0 + 1j, fNyquist = fs / 2 at -1 + 0j
# . use whole=False to have only positve frequencies, so 0 to fs / 2
# . use whole=False to have only posit
i
ve frequencies, so 0 to fs / 2
# . use Nfreq frequency points
fig2
,
ax2
=
plt
.
subplots
(
1
)
if
'
power spectrum
'
in
show
or
'
phase spectrum
'
in
show
:
Nfreq
=
1024
[
f
,
HF
]
=
signal
.
freqz
(
b
,
a
,
Nfreq
,
whole
=
False
,
fs
=
fs
)
[
f
,
HF
]
=
signal
.
freqz
(
b
,
a
,
Nfreq
,
whole
=
whole
,
fs
=
fs
)
if
'
power spectrum
'
in
show
:
fig2
,
ax2
=
plt
.
subplots
(
1
)
ax2
.
plot
(
f
,
pow_db
(
HF
))
ax2
.
set_title
(
'
Power spectrum
'
)
ax2
.
set_xlabel
(
'
frequency [fs = %f]
'
%
fs
)
ax2
.
set_ylabel
(
'
HF power [dB]
'
)
if
'
phase spectrum
'
in
show
:
fig3
,
ax3
=
plt
.
subplots
(
1
)
ax3
.
plot
(
f
,
np
.
unwrap
(
np
.
angle
(
HF
)))
ax3
.
set_title
(
'
Phase spectrum
'
)
ax3
.
set_xlabel
(
'
frequency [fs = %f]
'
%
fs
)
ax3
.
set_ylabel
(
'
HF phase [rad]
'
)
# Plot impulse response
if
'
time response
'
in
show
:
Ts
=
1
/
fs
fig3
,
ax3
=
plt
.
subplots
(
1
)
step
=
True
# step response (makes impz use np.cumsum(h))
step
=
False
[
h
,
t
]
=
dsp_fpga_lib
.
impz
(
b
,
a
,
FS
=
fs
,
N
=
Ntime
,
step
=
step
)
# uses signal.lfilter()
(
ml
,
sl
,
bl
)
=
ax3
.
stem
(
t
,
h
,
linefmt
=
'
b-
'
,
markerfmt
=
'
ro
'
,
basefmt
=
'
k
'
)
ax3
.
set_xlabel
(
'
time [Ts = %f]
'
%
Ts
)
ax3
.
set_ylabel
(
'
h[n]
'
)
fig4
,
ax4
=
plt
.
subplots
(
1
)
# impz uses signal.lfilter(), and cumsum(h) for step response
[
h
,
t
]
=
dsp_fpga_lib
.
impz
(
b
,
a
,
FS
=
fs
,
N
=
Ntime
,
step
=
step
)
(
ml
,
sl
,
bl
)
=
ax4
.
stem
(
t
,
h
,
linefmt
=
'
b-
'
,
markerfmt
=
'
ro
'
,
basefmt
=
'
k
'
)
if
step
:
ax4
.
set_title
(
'
Step response
'
)
else
:
ax4
.
set_title
(
'
Impulse response
'
)
ax4
.
set_xlabel
(
'
time [Ts = %f]
'
%
Ts
)
ax4
.
set_ylabel
(
'
h[n]
'
)
return
z
,
p
,
k
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment