Skip to content

Instantly share code, notes, and snippets.

@mattdsp
mattdsp / eqnerror.m
Last active November 8, 2023 10:04
Equation error method for digital IIR filter design; optional iterative solution of exact least squares filter design problem
function [a,b] = eqnerror(M,N,w,D,W,iter);
% [a,b] = eqnerror(M,N,w,D,W,iter);
%
% IIR filter design using equation error method
%
% if the input argument 'iter' is specified and if iter > 1, then
% the equation error method is applied iteratively trying to
% determine the true L2 solution of the nonlinear IIR design problem
%
% M numerator order
@mattdsp
mattdsp / cfirls.m
Last active January 17, 2024 12:06
Least-squares FIR filter design with prescribed magnitude and phase responses and with possibly complex coefficients
function h = cfirls(N,f,d,w,hreal)
% function h = cfirls(N,f,d,w,hreal)
% Least-squares FIR filter design with prescribed magnitude and phase
% responses and with possibly complex coefficients
%
% h real or complex-valued impulse response
% N desired filter length
% f frequency grid
% for complex-valued filters ('hreal=0'): -1 <= f < 1
% ( '1' corresponds to Nyquist) or 0 <= f < 2
@mattdsp
mattdsp / levin.m
Last active January 17, 2024 12:07
Least squares FIR digital filter design with prescribed magnitude AND phase response using Levinson's algorithm
function x = levin(a,b)
% function x = levin(a,b)
% solves system of complex linear equations toeplitz(a)*x=b
% using Levinson's algorithm
% a ... first row of positive definite Hermitian Toeplitz matrix
% b ... right hand side vector
%
% Author: Mathias C. Lang, Vienna University of Technology, AUSTRIA
% 1997-09
% mattsdspblog@gmail.com
@mattdsp
mattdsp / iir_ap.m
Last active June 20, 2023 18:29
IIR allpass filter design using an equation error method
function a = iir_ap(N,f,phi,W)
% function a = iir_ap(N,f,phi,W)
% IIR allpass filter design using an equation error method
%
% a denominator coefficients; numerator = flipud(a)
% N filter order
% f frequency grid; 0 <= f <= 1 ('1' equals Nyquist)
% phi desired phase on the grid (in radians)
% W positive weight function on the frequency grid
%
@mattdsp
mattdsp / fsamp.m
Created March 11, 2023 11:19
Frequency sampling design of linear phase FIR filters
function h = fsamp(mag,N)
% function h = fsamp(mag,N)
% frequency sampling design of linear phase FIR filter
%
% mag ... desired magnitude response on an equidistant grid
% between 0 and Nyquist
% N ... filter length (N <= 2*length(mag) - 1 )
%
% for even N the frequency grid is defined by (M = length(mag)):
%