Code Snippets Submitted by tjc
CIC Filter Analysis script
%==========================================================================
% CIC Filter Analysis Script
%
% Compute CIC filter frequency response, compensation filter coefficients,
% and compensated frequency response for a given set of design paramenters.
%
%
% Author: Tom Chatt
%
% Revision History:
%==========================================================================
% 09/23/2010 Tom Chatt Initial Version
%==========================================================================
clear all; close all; clc;
%=============================
% CIC Filter Design Parameters
%=============================
N = 3; % Sinc/Comb-Filter order
M = 1; % Stage delay (should be 1 or 2).
D = 64; % Decimation factor
fs = 10e6; % Pre-decimation sampling Frequency
fs_lo = fs / D; % Post-decimation sampling frequency
%==============================
% Compensator Design Parameters
%==============================
B = 12; % Compensator coefficient bit width
% fir2 parameters
fc = fs/D/2; % Ideal cutoff frequency
L = 16; % Filter order - 1. Must be even. Final filter length is L + 1.
Fo = D*fc/fs; % Normalized cutoff frequency
%=====================================================
% Generate and plot filters
%
% Floating point compensator filter coef. stored in h
% Fixed point compensator filter coef. stored in hz
%
%======================================================
% CIC Compensator using fir2
p = 2e3; % Granularity
s = 0.25 / p; % Step size
fpass = 0:s:Fo; % Pass band frequency samples
fstop = (Fo + s) : s : 0.5; % Stop band frequency samples
ff = [fpass fstop] * 2; % Normalized frequency samples, 0 <= f <= 1
Mp = ones(1,length(fpass)); %% Pass band response; Mp(1)=1
Mp(2:end) = abs( M*D*sin(pi*fpass(2:end)/D)./sin(pi*M*fpass(2:end))).^N;
Mf = [Mp zeros(1,length(fstop))];
ff(end) = 1;
h = fir2(L,ff,Mf); %% Filter length L+1
h = h/max(h); %% Floating point coefficients
hz = round(h*power(2,B-1)-1)/(2^(B-1)); %% Fixed point coefficients
f = linspace(0,1,4*length(ff));
f_lo = [ff ff+1 ff+2 ff+3];
H = (D^-N*abs(D*M*sin(pi*M*D*f) ./ (pi*M*D*f)).^N);
HH = (D^-N*abs(D*M*sin(pi*M*ff) ./ (pi*M*ff)).^N);
HHH = (D^-N*abs(D*M*sin(pi*M*f_lo) ./ (pi*M*f_lo)).^N);
H_comp = abs(fft(hz,length(ff)));
%=========
% Plotting
%=========
figure('name','CIC Frequency Response','Numbertitle','off');
subplot(211);
plot(f*fs, db(H), 'b');
grid on;
title([{'Wideband frequency response of CIC filter'};{sprintf('D = %i, N = %i',D,N)}]);
xlabel('Frequency (Hz)');
ylabel('|H(e^{j\omega})| (dB)');
axis([0,fs,-1250,0]);
subplot(212);
plot(f_lo*fs_lo, db(HHH));
grid on;
title('Narrowband frequency response of CIC filter');
xlabel('Frequency (Hz)');
ylabel('|H(e^{j\omega})| (dB)');
axis([0,4*fs_lo,-200,5]);
figure('name','CIC with Compensator Filter','Numbertitle','off');
subplot(211);
hold on;
grid on;
plot(f_lo*fs_lo, db(HHH), 'b');
plot(f_lo*fs_lo, db([H_comp H_comp H_comp H_comp]), 'g');
plot(f_lo*fs_lo, db(HHH .* [H_comp H_comp H_comp H_comp]), 'r');
axis([0,4*fs_lo,-200,25])
title([{'CIC with Compensator FIR'};{sprintf('Compensator length = %i, Coefficient precision = %i',L,B)}]);
xlabel('Frequency (Hz)');
ylabel('|H(e^{j\omega})| (dB)');
subplot(212);
hold on;
grid on;
plot(ff*fs_lo, HH, 'b');
plot(ff*fs_lo, H_comp, 'g');
plot(ff*fs_lo, HH .* H_comp, 'r');
axis([0,1*fs_lo,0,1.1]);
title('Closeup of CIC + compensator passband');
xlabel('Frequency (Hz)');
ylabel('|H(e^{j\omega})|');
legend('CIC','Compensator Filter','Net Response','location','northeast');
Gray Code Generator - Matlab Script to Generate Verilog Header for Gray Coding
%==============================================================================
%
% Gray Code Generator for Verilog
%
% This script generates a Verilog header (.vh) file which contains a set of
% of `define directives to establish Gray-coded states for a state machine.
% The output of this script is intended to be used in conjunction with a
% Verilog design which uses Gray coding in some application. For more
% information on Gray coding, please see
% http://mathworld.wolfram.com/GrayCode.html.
%
% Usage:
%
% The bit width of the Gray coded states is set as a script variable.
% 2^bit_width states are generated. The output filename is also
% set as a script variable.
%
% Author: Tom Chatt
%
%------------------------------------------------------------------------------
% Revision History:
%
% Original Version Tom Chatt 11/2/2010
%
%==============================================================================
clear all; clc;
%------------------------------------------------------------------------------
bit_width = 8; % Bit width of the generated code
% Print the generated code entries to a Verilog defines file?
print_to_v_def_file = true;
output_file = ['gray_code_',num2str(bit_width),'.vh']; % Output file name
% Gray code state name prefix. State names will be of the form
% GC<bit_width>B_<state number>.
prefix = ['GC',num2str(bit_width),'B_'];
%--------------Do not edit code below this point-------------------------------
% Generate the code, starting with 1 bit code and looping upward
code = [0; 1];
if bit_width ~= 1
for i = 2 : bit_width
code = [zeros(size(code,1), 1), code; ones(size(code,1), 1), flipud(code)]; %#ok<AGROW>
end
end
% Print out the generated code
fid = fopen(output_file, 'w');
for i = 1 : size(code,1)
macro_name = [prefix, num2str(i)];
macro_val = [num2str(bit_width),'''', dec2bin(binvec2dec(code(i,:)),bit_width)];
fprintf(fid, ['ifndef ',macro_name,'\n']);
fprintf(fid, [' ','`define ', macro_name, ' ', macro_val, '\n']);
fprintf(fid, 'endif\n');
end
fclose('all');
Gray Code Generator - Matlab Script to Generate Verilog Header for Gray Coding
%==============================================================================
%
% Gray Code Generator for Verilog
%
% This script generates a Verilog header (.vh) file which contains a set of
% of `define directives to establish Gray-coded states for a state machine.
% The output of this script is intended to be used in conjunction with a
% Verilog design which uses Gray coding in some application. For more
% information on Gray coding, please see
% http://mathworld.wolfram.com/GrayCode.html.
%
% Usage:
%
% The bit width of the Gray coded states is set as a script variable.
% 2^bit_width states are generated. The output filename is also
% set as a script variable.
%
% Author: Tom Chatt
%
%------------------------------------------------------------------------------
% Revision History:
%
% Original Version Tom Chatt 11/2/2010
%
%==============================================================================
clear all; clc;
%------------------------------------------------------------------------------
bit_width = 8; % Bit width of the generated code
% Print the generated code entries to a Verilog defines file?
print_to_v_def_file = true;
output_file = ['gray_code_',num2str(bit_width),'.vh']; % Output file name
% Gray code state name prefix. State names will be of the form
% GC<bit_width>B_<state number>.
prefix = ['GC',num2str(bit_width),'B_'];
%--------------Do not edit code below this point-------------------------------
% Generate the code, starting with 1 bit code and looping upward
code = [0; 1];
if bit_width ~= 1
for i = 2 : bit_width
code = [zeros(size(code,1), 1), code; ones(size(code,1), 1), flipud(code)]; %#ok<AGROW>
end
end
% Print out the generated code
fid = fopen(output_file, 'w');
for i = 1 : size(code,1)
macro_name = [prefix, num2str(i)];
macro_val = [num2str(bit_width),'''', dec2bin(binvec2dec(code(i,:)),bit_width)];
fprintf(fid, ['ifndef ',macro_name,'\n']);
fprintf(fid, [' ','`define ', macro_name, ' ', macro_val, '\n']);
fprintf(fid, 'endif\n');
end
fclose('all');
CIC Filter Analysis script
%==========================================================================
% CIC Filter Analysis Script
%
% Compute CIC filter frequency response, compensation filter coefficients,
% and compensated frequency response for a given set of design paramenters.
%
%
% Author: Tom Chatt
%
% Revision History:
%==========================================================================
% 09/23/2010 Tom Chatt Initial Version
%==========================================================================
clear all; close all; clc;
%=============================
% CIC Filter Design Parameters
%=============================
N = 3; % Sinc/Comb-Filter order
M = 1; % Stage delay (should be 1 or 2).
D = 64; % Decimation factor
fs = 10e6; % Pre-decimation sampling Frequency
fs_lo = fs / D; % Post-decimation sampling frequency
%==============================
% Compensator Design Parameters
%==============================
B = 12; % Compensator coefficient bit width
% fir2 parameters
fc = fs/D/2; % Ideal cutoff frequency
L = 16; % Filter order - 1. Must be even. Final filter length is L + 1.
Fo = D*fc/fs; % Normalized cutoff frequency
%=====================================================
% Generate and plot filters
%
% Floating point compensator filter coef. stored in h
% Fixed point compensator filter coef. stored in hz
%
%======================================================
% CIC Compensator using fir2
p = 2e3; % Granularity
s = 0.25 / p; % Step size
fpass = 0:s:Fo; % Pass band frequency samples
fstop = (Fo + s) : s : 0.5; % Stop band frequency samples
ff = [fpass fstop] * 2; % Normalized frequency samples, 0 <= f <= 1
Mp = ones(1,length(fpass)); %% Pass band response; Mp(1)=1
Mp(2:end) = abs( M*D*sin(pi*fpass(2:end)/D)./sin(pi*M*fpass(2:end))).^N;
Mf = [Mp zeros(1,length(fstop))];
ff(end) = 1;
h = fir2(L,ff,Mf); %% Filter length L+1
h = h/max(h); %% Floating point coefficients
hz = round(h*power(2,B-1)-1)/(2^(B-1)); %% Fixed point coefficients
f = linspace(0,1,4*length(ff));
f_lo = [ff ff+1 ff+2 ff+3];
H = (D^-N*abs(D*M*sin(pi*M*D*f) ./ (pi*M*D*f)).^N);
HH = (D^-N*abs(D*M*sin(pi*M*ff) ./ (pi*M*ff)).^N);
HHH = (D^-N*abs(D*M*sin(pi*M*f_lo) ./ (pi*M*f_lo)).^N);
H_comp = abs(fft(hz,length(ff)));
%=========
% Plotting
%=========
figure('name','CIC Frequency Response','Numbertitle','off');
subplot(211);
plot(f*fs, db(H), 'b');
grid on;
title([{'Wideband frequency response of CIC filter'};{sprintf('D = %i, N = %i',D,N)}]);
xlabel('Frequency (Hz)');
ylabel('|H(e^{j\omega})| (dB)');
axis([0,fs,-1250,0]);
subplot(212);
plot(f_lo*fs_lo, db(HHH));
grid on;
title('Narrowband frequency response of CIC filter');
xlabel('Frequency (Hz)');
ylabel('|H(e^{j\omega})| (dB)');
axis([0,4*fs_lo,-200,5]);
figure('name','CIC with Compensator Filter','Numbertitle','off');
subplot(211);
hold on;
grid on;
plot(f_lo*fs_lo, db(HHH), 'b');
plot(f_lo*fs_lo, db([H_comp H_comp H_comp H_comp]), 'g');
plot(f_lo*fs_lo, db(HHH .* [H_comp H_comp H_comp H_comp]), 'r');
axis([0,4*fs_lo,-200,25])
title([{'CIC with Compensator FIR'};{sprintf('Compensator length = %i, Coefficient precision = %i',L,B)}]);
xlabel('Frequency (Hz)');
ylabel('|H(e^{j\omega})| (dB)');
subplot(212);
hold on;
grid on;
plot(ff*fs_lo, HH, 'b');
plot(ff*fs_lo, H_comp, 'g');
plot(ff*fs_lo, HH .* H_comp, 'r');
axis([0,1*fs_lo,0,1.1]);
title('Closeup of CIC + compensator passband');
xlabel('Frequency (Hz)');
ylabel('|H(e^{j\omega})|');
legend('CIC','Compensator Filter','Net Response','location','northeast');