You are viewing an older revision! See the latest version

Matlab FIR Filter

Design a simple set of filter coefficients, apply it to a test signal and visualise results

Create a test signal

320 samples at 48kHz for a sum of two sinusoids (1000Hz + 15000 Hz)

sample_rate = 48000;
nsamples = 256;

F = [1 15] * 1000;
A = [1 0.5];

% Time vector - use colon operator to generate integer vector of sample
% numbers
t = (0:nsamples-1) / sample_rate;

% Test signal - use matrix notation to compose it with single expression
signal = A * sin(2*pi*F'*t);

Create a FIR filter and apply it to test signal

Assume a lowpass filter with cutoff frequency of 6 kHz. The expectation is this should filter out the 15 kHz component from the test signal

% Choose filter cutoff frequency (6 kHz)
cutoff_hz = 6000;

% Normalize cutoff frequency (wrt Nyquist frequency)
nyq_freq = sample_rate / 2;
cutoff_norm = cutoff_hz / nyq_freq;

% FIR filter order (i.e. number of coefficients - 1)
order = 28;

% Create lowpass FIR filter through a direct approach: provide
% (normalized) cutoff frequency and filter order (assumed as known).
% fir1 takes care of designing the filter by imposing the constraints in
% the frequency domain and transforming back to time using a given window
% (the dafault used here is the Hamming window).
% For more advanced requirements see e.g. firpmord and firpm
% NOTE: fir1, firpmord and firpm all require Signal Processing Toolbox
fir_coeff = fir1(order, cutoff_norm);

% Analyse the filter using the Filter Visualization Tool
fvtool(fir_coeff, 'Fs', sample_rate)

% Filter the signal with the FIR filter
filtered_signal = filter(fir_coeff, 1, signal);

/media/uploads/emilmont/fir_design_01.png

Plot the original and filtered signals

Also align filtered signal with original and discard transient samples (in this case the first order samples)

% Group delay as a scalar, in number of samples
group_delay = median(grpdelay(fir_coeff));

% Group delay in seconds
group_delay_s = group_delay / sample_rate;


% Plot the original signal...
figure(1)
plot(t, signal)
% ...and allow adding more plots
hold on

% Align and plot the filtered signal
% (On top of existing one)
plot(t-group_delay_s, filtered_signal, 'r-')

% Align and plot only the d6esired part of the filtered signal (discarding
% the transient)
plot(t(order:end)-group_delay_s, filtered_signal(order:end), ...
    'g', 'LineWidth', 4);

grid on
hold off

/media/uploads/emilmont/fir_design_02.png

Manually export available data to C expressions

Diplay C-like definitions for available data as a single-line strings

% Use custom helper function vectorToCDefFloat32_t on filter coefficients,
% initial test signal and filtered signal, respectively
disp(vectorToCDefFloat32_t( 'fir_coeff', fir_coeff) )
disp(vectorToCDefFloat32_t( 'signal', signal) )
disp(vectorToCDefFloat32_t( 'filtered_signal', filtered_signal) )Manually export available data to C expressions

const float32_t fir_coeff[29] 	= {
	-0.0018225231f, -0.0015879293f, +0.0000000000f, +0.0036977509f, +0.0080754301f, 
	+0.0085302219f, -0.0000000000f, -0.0173976980f, -0.0341458619f, -0.0333591551f, 
	+0.0000000000f, +0.0676308423f, +0.1522061825f, +0.2229246944f, +0.2504960895f, 
	+0.2229246944f, +0.1522061825f, +0.0676308423f, +0.0000000000f, -0.0333591551f, 
	-0.0341458619f, -0.0173976980f, -0.0000000000f, +0.0085302219f, +0.0080754301f, 
	+0.0036977509f, +0.0000000000f, -0.0015879293f, -0.0018225231f
};

const float32_t signal[256] 	= {
	+0.0000000000f, +0.5924659371f, -0.0947343484f, +0.1913417131f, +1.0000000000f, 
	+0.4174197018f, +0.3535533845f, +1.2552931309f, +0.8660253882f, +0.4619397521f, 
	+1.3194792271f, +1.1827865839f, +0.5000000000f, +1.1827865839f, +1.3194792271f, 
	+0.4619397521f, +0.8660253882f, +1.2552931309f, +0.3535533845f, +0.4174197018f, 
	+1.0000000000f, +0.1913417131f, -0.0947343484f, +0.5924659371f, -0.0000000000f, 
	-0.5924659371f, +0.0947343484f, -0.1913417131f, -1.0000000000f, -0.4174197018f, 
	-0.3535533845f, -1.2552931309f, -0.8660253882f, -0.4619397521f, -1.3194792271f, 
	-1.1827865839f, -0.5000000000f, -1.1827865839f, -1.3194792271f, -0.4619397521f, 
	-0.8660253882f, -1.2552931309f, -0.3535533845f, -0.4174197018f, -1.0000000000f, 
	-0.1913417131f, +0.0947343484f, -0.5924659371f, +0.0000000000f, +0.5924659371f, 
	-0.0947343484f, +0.1913417131f, +1.0000000000f, +0.4174197018f, +0.3535533845f, 
	+1.2552931309f, +0.8660253882f, +0.4619397521f, +1.3194792271f, +1.1827865839f, 
	+0.5000000000f, +1.1827865839f, +1.3194792271f, +0.4619397521f, +0.8660253882f, 
	+1.2552931309f, +0.3535533845f, +0.4174197018f, +1.0000000000f, +0.1913417131f, 
	-0.0947343484f, +0.5924659371f, +0.0000000000f, -0.5924659371f, +0.0947343484f, 
	-0.1913417131f, -1.0000000000f, -0.4174197018f, -0.3535533845f, -1.2552931309f, 
	-0.8660253882f, -0.4619397521f, -1.3194792271f, -1.1827865839f, -0.5000000000f, 
	-1.1827865839f, -1.3194792271f, -0.4619397521f, -0.8660253882f, -1.2552931309f, 
	-0.3535533845f, -0.4174197018f, -1.0000000000f, -0.1913417131f, +0.0947343484f, 
	-0.5924659371f, +0.0000000000f, +0.5924659371f, -0.0947343484f, +0.1913417131f, 
	+1.0000000000f, +0.4174197018f, +0.3535533845f, +1.2552931309f, +0.8660253882f, 
	+0.4619397521f, +1.3194792271f, +1.1827865839f, +0.5000000000f, +1.1827865839f, 
	+1.3194792271f, +0.4619397521f, +0.8660253882f, +1.2552931309f, +0.3535533845f, 
	+0.4174197018f, +1.0000000000f, +0.1913417131f, -0.0947343484f, +0.5924659371f, 
	+0.0000000000f, -0.5924659371f, +0.0947343484f, -0.1913417131f, -1.0000000000f, 
	-0.4174197018f, -0.3535533845f, -1.2552931309f, -0.8660253882f, -0.4619397521f, 
	-1.3194792271f, -1.1827865839f, -0.5000000000f, -1.1827865839f, -1.3194792271f, 
	-0.4619397521f, -0.8660253882f, -1.2552931309f, -0.3535533845f, -0.4174197018f, 
	-1.0000000000f, -0.1913417131f, +0.0947343484f, -0.5924659371f, -0.0000000000f, 
	+0.5924659371f, -0.0947343484f, +0.1913417131f, +1.0000000000f, +0.4174197018f, 
	+0.3535533845f, +1.2552931309f, +0.8660253882f, +0.4619397521f, +1.3194792271f, 
	+1.1827865839f, +0.5000000000f, +1.1827865839f, +1.3194792271f, +0.4619397521f, 
	+0.8660253882f, +1.2552931309f, +0.3535533845f, +0.4174197018f, +1.0000000000f, 
	+0.1913417131f, -0.0947343484f, +0.5924659371f, -0.0000000000f, -0.5924659371f, 
	+0.0947343484f, -0.1913417131f, -1.0000000000f, -0.4174197018f, -0.3535533845f, 
	-1.2552931309f, -0.8660253882f, -0.4619397521f, -1.3194792271f, -1.1827865839f, 
	-0.5000000000f, -1.1827865839f, -1.3194792271f, -0.4619397521f, -0.8660253882f, 
	-1.2552931309f, -0.3535533845f, -0.4174197018f, -1.0000000000f, -0.1913417131f, 
	+0.0947343484f, -0.5924659371f, +0.0000000000f, +0.5924659371f, -0.0947343484f, 
	+0.1913417131f, +1.0000000000f, +0.4174197018f, +0.3535533845f, +1.2552931309f, 
	+0.8660253882f, +0.4619397521f, +1.3194792271f, +1.1827865839f, +0.5000000000f, 
	+1.1827865839f, +1.3194792271f, +0.4619397521f, +0.8660253882f, +1.2552931309f, 
	+0.3535533845f, +0.4174197018f, +1.0000000000f, +0.1913417131f, -0.0947343484f, 
	+0.5924659371f, +0.0000000000f, -0.5924659371f, +0.0947343484f, -0.1913417131f, 
	-1.0000000000f, -0.4174197018f, -0.3535533845f, -1.2552931309f, -0.8660253882f, 
	-0.4619397521f, -1.3194792271f, -1.1827865839f, -0.5000000000f, -1.1827865839f, 
	-1.3194792271f, -0.4619397521f, -0.8660253882f, -1.2552931309f, -0.3535533845f, 
	-0.4174197018f, -1.0000000000f, -0.1913417131f, +0.0947343484f, -0.5924659371f, 
	-0.0000000000f, +0.5924659371f, -0.0947343484f, +0.1913417131f, +1.0000000000f, 
	+0.4174197018f, +0.3535533845f, +1.2552931309f, +0.8660253882f, +0.4619397521f, 
	+1.3194792271f, +1.1827865839f, +0.5000000000f, +1.1827865839f, +1.3194792271f, 
	+0.4619397521f
};

const float32_t filtered_signal[256] 	= {
	+0.0000000000f, -0.0010797828f, -0.0007681386f, -0.0001982932f, +0.0000644313f, 
	+0.0020854271f, +0.0036891871f, +0.0015855941f, -0.0026280805f, -0.0075907656f, 
	-0.0119390534f, -0.0086665964f, +0.0088981204f, +0.0430539288f, +0.0974468738f, 
	+0.1740405560f, +0.2681416571f, +0.3747720122f, +0.4893362224f, +0.6024154425f, 
	+0.7058740854f, +0.7968348861f, +0.8715901971f, +0.9277881384f, +0.9682182670f, 
	+0.9934674501f, +1.0012052059f, +0.9925859571f, +0.9681538343f, +0.9257026911f, 
	+0.8679010272f, +0.7952492833f, +0.7085021734f, +0.6100062132f, +0.5012753010f, 
	+0.3834386170f, +0.2592435479f, +0.1309866309f, -0.0000000000f, -0.1309866309f, 
	-0.2592435479f, -0.3834386170f, -0.5012753010f, -0.6100062132f, -0.7085021734f, 
	-0.7952492833f, -0.8679010272f, -0.9257026911f, -0.9681538343f, -0.9936656952f, 
	-1.0019733906f, -0.9936656952f, -0.9681538343f, -0.9257026911f, -0.8679010272f, 
	-0.7952492833f, -0.7085021734f, -0.6100062132f, -0.5012753010f, -0.3834386170f, 
	-0.2592435479f, -0.1309866309f, +0.0000000000f, +0.1309866309f, +0.2592435479f, 
	+0.3834386170f, +0.5012753010f, +0.6100062132f, +0.7085021734f, +0.7952492833f, 
	+0.8679010272f, +0.9257026911f, +0.9681538343f, +0.9936656952f, +1.0019733906f, 
	+0.9936656952f, +0.9681538343f, +0.9257026911f, +0.8679010272f, +0.7952492833f, 
	+0.7085021734f, +0.6100062132f, +0.5012753010f, +0.3834386170f, +0.2592435479f, 
	+0.1309866309f, -0.0000000000f, -0.1309866309f, -0.2592435479f, -0.3834386170f, 
	-0.5012753010f, -0.6100062132f, -0.7085021734f, -0.7952492833f, -0.8679010272f, 
	-0.9257026911f, -0.9681538343f, -0.9936656952f, -1.0019733906f, -0.9936656952f, 
	-0.9681538343f, -0.9257026911f, -0.8679010272f, -0.7952492833f, -0.7085021734f, 
	-0.6100062132f, -0.5012753010f, -0.3834386170f, -0.2592435479f, -0.1309866309f, 
	+0.0000000000f, +0.1309866309f, +0.2592435479f, +0.3834386170f, +0.5012753010f, 
	+0.6100062132f, +0.7085021734f, +0.7952492833f, +0.8679010272f, +0.9257026911f, 
	+0.9681538343f, +0.9936656952f, +1.0019733906f, +0.9936656952f, +0.9681538343f, 
	+0.9257026911f, +0.8679010272f, +0.7952492833f, +0.7085021734f, +0.6100062132f, 
	+0.5012753010f, +0.3834386170f, +0.2592435479f, +0.1309866309f, +0.0000000000f, 
	-0.1309866309f, -0.2592435479f, -0.3834386170f, -0.5012753010f, -0.6100062132f, 
	-0.7085021734f, -0.7952492833f, -0.8679010272f, -0.9257026911f, -0.9681538343f, 
	-0.9936656952f, -1.0019733906f, -0.9936656952f, -0.9681538343f, -0.9257026911f, 
	-0.8679010272f, -0.7952492833f, -0.7085021734f, -0.6100062132f, -0.5012753010f, 
	-0.3834386170f, -0.2592435479f, -0.1309866309f, +0.0000000000f, +0.1309866309f, 
	+0.2592435479f, +0.3834386170f, +0.5012753010f, +0.6100062132f, +0.7085021734f, 
	+0.7952492833f, +0.8679010272f, +0.9257026911f, +0.9681538343f, +0.9936656952f, 
	+1.0019733906f, +0.9936656952f, +0.9681538343f, +0.9257026911f, +0.8679010272f, 
	+0.7952492833f, +0.7085021734f, +0.6100062132f, +0.5012753010f, +0.3834386170f, 
	+0.2592435479f, +0.1309866309f, -0.0000000000f, -0.1309866309f, -0.2592435479f, 
	-0.3834386170f, -0.5012753010f, -0.6100062132f, -0.7085021734f, -0.7952492833f, 
	-0.8679010272f, -0.9257026911f, -0.9681538343f, -0.9936656952f, -1.0019733906f, 
	-0.9936656952f, -0.9681538343f, -0.9257026911f, -0.8679010272f, -0.7952492833f, 
	-0.7085021734f, -0.6100062132f, -0.5012753010f, -0.3834386170f, -0.2592435479f, 
	-0.1309866309f, +0.0000000000f, +0.1309866309f, +0.2592435479f, +0.3834386170f, 
	+0.5012753010f, +0.6100062132f, +0.7085021734f, +0.7952492833f, +0.8679010272f, 
	+0.9257026911f, +0.9681538343f, +0.9936656952f, +1.0019733906f, +0.9936656952f, 
	+0.9681538343f, +0.9257026911f, +0.8679010272f, +0.7952492833f, +0.7085021734f, 
	+0.6100062132f, +0.5012753010f, +0.3834386170f, +0.2592435479f, +0.1309866309f, 
	+0.0000000000f, -0.1309866309f, -0.2592435479f, -0.3834386170f, -0.5012753010f, 
	-0.6100062132f, -0.7085021734f, -0.7952492833f, -0.8679010272f, -0.9257026911f, 
	-0.9681538343f, -0.9936656952f, -1.0019733906f, -0.9936656952f, -0.9681538343f, 
	-0.9257026911f, -0.8679010272f, -0.7952492833f, -0.7085021734f, -0.6100062132f, 
	-0.5012753010f, -0.3834386170f, -0.2592435479f, -0.1309866309f, -0.0000000000f, 
	+0.1309866309f
};

All wikipages