-
Notifications
You must be signed in to change notification settings - Fork 33
add limits calculation and helper script to plot the frequency response #603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
904ce97
add limits calculation and helper script to plot the frequency response
nkrackow 4b4187d
address some lints and imports in workflow
nkrackow 6427cfe
address CI
nkrackow de3ec49
address comments
nkrackow 748755d
add lint comment
nkrackow e7eb439
add more lint comments
nkrackow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| #!/usr/bin/python3 | ||
| """ | ||
| Small tool to show the frequency response of biquad IIR filters. | ||
| """ | ||
| import numpy as np | ||
| import matplotlib.pyplot as plt | ||
| from scipy import signal | ||
| import argparse | ||
|
|
||
| from iir_coefficients import get_filters | ||
| import stabilizer | ||
|
|
||
|
|
||
| def _main(): | ||
| parser = argparse.ArgumentParser( | ||
| description="Plot frequency response for filter parameters" | ||
| ) | ||
| parser.add_argument( | ||
| "--sample-period", | ||
| "-s", | ||
| type=float, | ||
| default=stabilizer.SAMPLE_PERIOD, | ||
| help="Sample period in seconds (%(default)s s)", | ||
| ) | ||
|
|
||
| # Next, add subparsers and their arguments. | ||
| subparsers = parser.add_subparsers( | ||
| help="Filter-specific design parameters", dest="filter_type", required=True | ||
| ) | ||
|
|
||
| filters = get_filters() | ||
|
|
||
| for (filter_name, filt) in filters.items(): | ||
| subparser = subparsers.add_parser(filter_name, help=filt.help) | ||
| for arg in filt.arguments: | ||
| subparser.add_argument(*arg.positionals, **arg.keywords) | ||
|
nkrackow marked this conversation as resolved.
|
||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| # Calculate the IIR coefficients for the filter. | ||
| coefficients = filters[args.filter_type].coefficients(args) | ||
|
|
||
| print(coefficients) | ||
|
|
||
| # The feed-forward gain of the IIR filter is the summation | ||
| # of the "b" components of the filter. | ||
| forward_gain = sum(coefficients[:3]) | ||
| if forward_gain == 0 and args.x_offset != 0: | ||
| print("Filter has no DC gain but x_offset is non-zero") | ||
|
|
||
| f = np.logspace(-7, np.log10(0.5 / args.sample_period), 1024, endpoint=False) | ||
|
jordens marked this conversation as resolved.
Outdated
|
||
| f, h = signal.freqz( | ||
| coefficients[:3], | ||
| [1] + [-c for c in coefficients[3:]], | ||
|
jordens marked this conversation as resolved.
Outdated
|
||
| worN=f, | ||
| fs=1 / args.sample_period, | ||
| ) | ||
| fig, ax = plt.subplots() | ||
| ax.margins(0, 0.1) | ||
|
nkrackow marked this conversation as resolved.
Outdated
|
||
| ax.plot(f, 20 * np.log10(abs(h))) | ||
|
nkrackow marked this conversation as resolved.
Outdated
|
||
| ax.set_xscale("log") | ||
| ax.grid() | ||
| ax.set_xlabel("Frequency (Hz)") | ||
| ax.set_ylabel("Magnitude (dB)") | ||
| ax.set_title("Filter response") | ||
| plt.show() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| _main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.