mirror of
https://github.com/XGudron/UA3REO-DDC-Transceiver.git
synced 2025-08-08 09:01:04 +03:00
9.4.0 version
This commit is contained in:
1
Stuff/CICComp/rx.coe
Normal file
1
Stuff/CICComp/rx.coe
Normal file
@@ -0,0 +1 @@
|
||||
2.588184e+01,-1.177676e+01,-4.247139e+01,1.462374e+01,6.498443e+01,-1.619333e+01,-9.530045e+01,1.543182e+01,1.357651e+02,-1.075484e+01,-1.892556e+02,-1.792373e-01,2.591529e+02,2.065105e+01,-3.493771e+02,-5.522246e+01,4.642181e+02,1.099167e+02,-6.083312e+02,-1.926859e+02,7.863470e+02,3.135165e+02,-1.002837e+03,-4.850395e+02,1.261600e+03,7.224344e+02,-1.565650e+03,-1.044216e+03,1.916052e+03,1.471749e+03,-2.312049e+03,-2.030281e+03,2.749287e+03,2.747798e+03,-3.220302e+03,-3.656454e+03,3.711905e+03,4.790396e+03,-4.206394e+03,-6.187838e+03,4.677782e+03,7.887381e+03,-5.094268e+03,-9.931155e+03,5.412869e+03,1.235898e+04,-5.583896e+03,-1.521326e+04,5.543345e+03,1.853010e+04,-5.220412e+03,-2.234694e+04,4.526717e+03,2.668958e+04,-3.368222e+03,-3.158384e+04,1.629933e+03,3.703699e+04,8.059290e+02,-4.305550e+04,-4.081676e+03,4.961868e+04,8.337356e+03,-5.670522e+04,-1.374161e+04,6.425633e+04,2.045314e+04,-7.221442e+04,-2.866435e+04,8.047206e+04,3.854700e+04,-8.892747e+04,-5.031376e+04,9.741395e+04,6.414251e+04,-1.057785e+05,-8.026279e+04,1.137843e+05,9.885155e+04,-1.212209e+05,-1.201540e+05,1.277694e+05,1.443408e+05,-1.331550e+05,-1.716768e+05,1.369615e+05,2.023263e+05,-1.388398e+05,-2.365878e+05,1.382513e+05,2.746292e+05,-1.347533e+05,-3.168155e+05,1.276402e+05,3.633474e+05,-1.163329e+05,-4.147242e+05,9.986966e+04,4.712470e+05,-7.743817e+04,-5.336842e+05,4.763423e+04,6.025870e+05,-9.184515e+03,-6.792728e+05,-4.017941e+04,7.648835e+05,1.027624e+05,-8.619200e+05,-1.827573e+05,9.729724e+05,2.851100e+05,-1.103365e+06,-4.190684e+05,1.259662e+06,5.977167e+05,-1.455176e+06,-8.472549e+05,1.709824e+06,1.214643e+06,-2.066687e+06,-1.812364e+06,2.608282e+06,2.938129e+06,-3.544861e+06,-5.781064e+06,5.218750e+06,1.945780e+07,1.945780e+07,5.218750e+06,-5.781064e+06,-3.544861e+06,2.938129e+06,2.608282e+06,-1.812364e+06,-2.066687e+06,1.214643e+06,1.709824e+06,-8.472549e+05,-1.455176e+06,5.977167e+05,1.259662e+06,-4.190684e+05,-1.103365e+06,2.851100e+05,9.729724e+05,-1.827573e+05,-8.619200e+05,1.027624e+05,7.648835e+05,-4.017941e+04,-6.792728e+05,-9.184515e+03,6.025870e+05,4.763423e+04,-5.336842e+05,-7.743817e+04,4.712470e+05,9.986966e+04,-4.147242e+05,-1.163329e+05,3.633474e+05,1.276402e+05,-3.168155e+05,-1.347533e+05,2.746292e+05,1.382513e+05,-2.365878e+05,-1.388398e+05,2.023263e+05,1.369615e+05,-1.716768e+05,-1.331550e+05,1.443408e+05,1.277694e+05,-1.201540e+05,-1.212209e+05,9.885155e+04,1.137843e+05,-8.026279e+04,-1.057785e+05,6.414251e+04,9.741395e+04,-5.031376e+04,-8.892747e+04,3.854700e+04,8.047206e+04,-2.866435e+04,-7.221442e+04,2.045314e+04,6.425633e+04,-1.374161e+04,-5.670522e+04,8.337356e+03,4.961868e+04,-4.081676e+03,-4.305550e+04,8.059290e+02,3.703699e+04,1.629933e+03,-3.158384e+04,-3.368222e+03,2.668958e+04,4.526717e+03,-2.234694e+04,-5.220412e+03,1.853010e+04,5.543345e+03,-1.521326e+04,-5.583896e+03,1.235898e+04,5.412869e+03,-9.931155e+03,-5.094268e+03,7.887381e+03,4.677782e+03,-6.187838e+03,-4.206394e+03,4.790396e+03,3.711905e+03,-3.656454e+03,-3.220302e+03,2.747798e+03,2.749287e+03,-2.030281e+03,-2.312049e+03,1.471749e+03,1.916052e+03,-1.044216e+03,-1.565650e+03,7.224344e+02,1.261600e+03,-4.850395e+02,-1.002837e+03,3.135165e+02,7.863470e+02,-1.926859e+02,-6.083312e+02,1.099167e+02,4.642181e+02,-5.522246e+01,-3.493771e+02,2.065105e+01,2.591529e+02,-1.792373e-01,-1.892556e+02,-1.075484e+01,1.357651e+02,1.543182e+01,-9.530045e+01,-1.619333e+01,6.498443e+01,1.462374e+01,-4.247139e+01,-1.177676e+01,2.588184e+01
|
252
Stuff/CICComp/rx.m
Normal file
252
Stuff/CICComp/rx.m
Normal file
@@ -0,0 +1,252 @@
|
||||
%% ------------------------------------------------------------------------
|
||||
%
|
||||
% Title : test_cic.m
|
||||
% Author : Alexander Kapitanov
|
||||
% E-mail : sallador@bk.ru
|
||||
% Version : 1.0
|
||||
%
|
||||
% -------------------------------------------------------------------------
|
||||
%
|
||||
% Description :
|
||||
%
|
||||
% FIR filter compensator to correct freq response after CIC filter.
|
||||
%
|
||||
% -------------------------------------------------------------------------
|
||||
%
|
||||
% Version : 1.0
|
||||
% Date : 2017.06.03
|
||||
%
|
||||
%% ------------------------------------------------------------------------
|
||||
%
|
||||
% GNU GENERAL PUBLIC LICENSE
|
||||
% Version 3, 29 June 2007
|
||||
%
|
||||
% Copyright (c) 2018 Kapitanov Alexander
|
||||
%
|
||||
% This program is free software: you can redistribute it and/or modify
|
||||
% it under the terms of the GNU General Public License as published by
|
||||
% the Free Software Foundation, either version 3 of the License, or
|
||||
% (at your option) any later version.
|
||||
%
|
||||
% You should have received a copy of the GNU General Public License
|
||||
% along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
%
|
||||
% THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
% APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
% HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
% OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
% THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
% PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
% IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
% ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
%
|
||||
%% ------------------------------------------------------------------------
|
||||
|
||||
set(0, 'DefaultAxesFontSize', 11, 'DefaultAxesFontName', 'Times New Roman');
|
||||
set(0, 'DefaultTextFontSize', 11, 'DefaultTextFontName', 'Times New Roman');
|
||||
|
||||
close all;
|
||||
clear all;
|
||||
|
||||
%% ------------------------------------------------------------------------
|
||||
% ---- CIC Filter Parameters
|
||||
% ------------------------------------------------------------------------
|
||||
|
||||
R = 160; % Decimation factor 122.880mhz->768khz
|
||||
N = 5; % Number of stages
|
||||
M = 1; % Differential delay (only 1)
|
||||
|
||||
%% ------------------------------------------------------------------------
|
||||
% ---- FIR filter parameters
|
||||
% ------------------------------------------------------------------------
|
||||
|
||||
NFIR = 256; % Filter order, must be odd when Fo = 0.5 !!!
|
||||
Bc = 32; % Coef. Bit-width
|
||||
Fo = 0.245; % Normalized Cutoff: 0.2 < Fo < 0.5;
|
||||
BETA = 8; % BETA parameter for Kaiser window (if IS_WIND = 'Y')
|
||||
|
||||
%% ------------------------------------------------------------------------
|
||||
% ---- Save data parameters
|
||||
% ------------------------------------------------------------------------
|
||||
|
||||
IS_COE = 'Y'; % create *.COE Xilinx file
|
||||
IS_HDR = 'N'; % create *.H file (header)
|
||||
IS_WIND = 'Y'; % use Kaiser Window for FIR corrector
|
||||
|
||||
IS_PLOT_IDL = 'Y'; % plot ideal response
|
||||
IS_PLOT_FIR = 'Y'; % plot FIR filter IR
|
||||
IS_PLOT_ERR = 'Y'; % plot total error response in passband
|
||||
|
||||
%% ------------------------------------------------------------------------
|
||||
% ---- CIC Compensator Design
|
||||
% ------------------------------------------------------------------------
|
||||
|
||||
NFFT = 2^16; % FFT points for Freq Response (spectrum)
|
||||
STEP = 1/NFFT; % Step size
|
||||
w = -pi:2*pi/NFFT:pi-2*pi/NFFT;
|
||||
ff = 0:1/NFFT:1-1/NFFT;
|
||||
z = exp(1j * w);
|
||||
|
||||
Fc = 1/(2*R);
|
||||
Fr = Fo/R;
|
||||
|
||||
% 1 way:
|
||||
%HCIC = (1/R * (1-z.^(-R*M))./(1-z.^(-1))).^ N;
|
||||
% 2 way:
|
||||
HCIC = (R^-N*abs(1*M*sin(pi*M*R*ff) ./ sin(pi*ff)).^N);
|
||||
HCICdb = 20 * log10(abs(HCIC));
|
||||
|
||||
fp = [0:STEP:Fo]; % Pass band frequency dots
|
||||
fs = [(Fo+STEP):STEP:0.5]; % Stop band frequency dots
|
||||
f = [fp fs]*2; % Normalized frequency dots
|
||||
f(end) = 1;
|
||||
|
||||
% Calculate ideal response
|
||||
Mp = ones(1, length(fp)); % Pass band response; Mp(1) = 1
|
||||
Mp(2:end) = abs(M * R * sin(pi*fp(2:end)/R) ./ sin(pi*M*fp(2:end))).^(N);
|
||||
Mf = [Mp zeros(1, length(fs))];
|
||||
|
||||
if (IS_PLOT_IDL == 'Y')
|
||||
figure('name','FIR Ideal Response', 'Numbertitle', 'off')
|
||||
plot(f/2, Mf, '-.', 'LineWidth', 2, 'Color',[0 0 1]);
|
||||
|
||||
title([{'FIR Ideal Response'};{sprintf('Fo = %i',Fo)}]);
|
||||
xlabel ('Freq (\pi x rad / samples)');
|
||||
ylabel ('Magnitude');
|
||||
axis tight;
|
||||
legend([{sprintf('Fo = %i',Fo)}]);
|
||||
grid on;
|
||||
end
|
||||
|
||||
% Calculate FIR
|
||||
hFIR = fir2(NFIR-1, f, Mf); % Filter length NFIR
|
||||
hFIR = hFIR / max(hFIR); % Double coefficients
|
||||
hCOE = round(hFIR*(2^(Bc-1)-1)); % Fixed point coefficients
|
||||
|
||||
% Windowed FIR (Kaiser with BETA)
|
||||
if (IS_WIND == 'Y')
|
||||
WIND = kaiser(NFIR, BETA); % KAISER WINDOW IS USED!
|
||||
hWIND = fir1(NFIR-1, Fo/R, 'low', WIND);
|
||||
hNEW = hCOE .* hWIND;% conv2(hCOE,Hwind);
|
||||
hCOE = hNEW;
|
||||
end
|
||||
|
||||
if (IS_PLOT_FIR == 'Y')
|
||||
figure('name','FIR Response', 'Numbertitle', 'off')
|
||||
plot(hFIR, '-', 'LineWidth', 2, 'Color',[1 0 0]);
|
||||
|
||||
title([{'FIR Response'};{sprintf('Order = %i',NFIR)}]);
|
||||
xlabel ('Samples');
|
||||
ylabel ('Magnitude');
|
||||
axis tight;
|
||||
legend([{sprintf('Order = %i',NFIR)}]);
|
||||
grid on;
|
||||
end
|
||||
|
||||
hFFT = 20 * log10(abs(fft(hCOE, ceil(NFFT/R))));
|
||||
hFFT = hFFT - max(hFFT);
|
||||
|
||||
H_amp = repmat(hFFT, 1, R);
|
||||
H_amp = H_amp(1:length(ff));
|
||||
H_amp = H_amp - max(H_amp);
|
||||
|
||||
H_comp = HCICdb + H_amp;
|
||||
H_comp = H_comp - max(H_comp);
|
||||
|
||||
%% ------------------------------------------------------------------------
|
||||
% ---- Plot results
|
||||
% ------------------------------------------------------------------------
|
||||
|
||||
% ---- Figure #1
|
||||
figure('name','CIC/FIR Frequency Response', 'Numbertitle', 'off')
|
||||
plot(ff, HCICdb - max(HCICdb), '-.', 'LineWidth', 2, 'Color',[0 0 1]);
|
||||
hold on;
|
||||
|
||||
plot(ff, H_amp, '--', 'LineWidth', 2, 'Color',[0 0.4 0]);
|
||||
hold on;
|
||||
|
||||
plot(ff, H_comp, '-', 'LineWidth', 2, 'Color',[1 0 0]);
|
||||
hold on;
|
||||
|
||||
title([{'CIC, Comp. FIR and Result'};{sprintf('Filter Order = %i, Coef. width = %i',NFIR,Bc)}]);
|
||||
xlabel ('Freq (\pi x rad / samples)');
|
||||
ylabel ('Magnitude (dB)');
|
||||
axis([0 ff(NFFT)/2 -100 5]);
|
||||
line([Fr Fr], [-400 200], 'LineWidth', 1, 'linestyle', '--', 'Color', [0 0 0]);
|
||||
line([Fc Fc], [-400 200], 'LineWidth', 1, 'linestyle', '--', 'Color', [0 0 0]);
|
||||
line([2*Fc 2*Fc], [-400 200], 'LineWidth', 1, 'linestyle', '--', 'Color', [0 0 0]);
|
||||
legend('CIC filter','Comp. FIR','Sum Response','location','northeast');
|
||||
grid on;
|
||||
|
||||
% ---- Figure #2
|
||||
figure('name', 'CIC/FIR Frequency Response (Zoom)', 'Numbertitle', 'off')
|
||||
plot(ff, HCICdb - max(HCICdb), '-.', 'LineWidth', 2, 'Color',[0 0 1]);
|
||||
hold on;
|
||||
|
||||
plot(ff, H_amp, '--', 'LineWidth', 2, 'Color',[0 0.4 0]);
|
||||
hold on;
|
||||
|
||||
plot(ff, H_comp, '-', 'LineWidth', 2, 'Color',[1 0 0]);
|
||||
hold on;
|
||||
|
||||
title([{'CIC, Comp. FIR and Result'};{sprintf('Filter Order = %i, Coef. width = %i',NFIR,Bc)}]);
|
||||
xlabel ('Freq (\pi x rad / samples)');
|
||||
ylabel ('Magnitude (dB)');
|
||||
axis([0 ff(NFFT)/(2*R) -7 1]);
|
||||
line([Fr Fr], [-400 200], 'LineWidth', 1, 'linestyle', '--', 'Color', [0 0 0]);
|
||||
grid on;
|
||||
|
||||
%% ------------------------------------------------------------------------
|
||||
% ---- Passband irregularity
|
||||
% ------------------------------------------------------------------------
|
||||
if (IS_PLOT_ERR == 'Y')
|
||||
pass = ceil(0.85*length(H_comp)*Fr);
|
||||
stp = 0:(0.9*Fr)/pass:(0.9*Fr)-(0.9*Fr)/pass;
|
||||
stp = stp(2:pass);
|
||||
Irr = H_comp(2:pass);
|
||||
Ism = mean(Irr);
|
||||
Iav = (max(Irr)-min(Irr))/2;
|
||||
|
||||
figure('name', 'Passband irregularity', 'Numbertitle', 'off')
|
||||
plot(stp, Irr, '--', 'LineWidth', 2, 'Color',[0 0 1]);
|
||||
line([0 (0.9*Fr)], [Ism Ism], 'LineWidth', 2, 'linestyle', '-', 'Color', [0 0 0]);
|
||||
grid on;
|
||||
title([{'Passband irregularity'};{sprintf('Mean value = %f',Ism)};{sprintf('Freq error = %f (dB)',Iav)}]);
|
||||
xlabel ('Freq (\pi x rad / samples)');
|
||||
ylabel ('Magnitude (dB)');
|
||||
axis tight;
|
||||
%axis([0 ff(NFFT)/(2*R) -0.05 1]);
|
||||
end
|
||||
|
||||
%% ------------------------------------------------------------------------
|
||||
% ---- Save coe data to files
|
||||
% ------------------------------------------------------------------------
|
||||
if (IS_COE == 'Y')
|
||||
fid = fopen ('rx.coe', 'w');
|
||||
fprintf(fid, 'Radix = 10;\n');
|
||||
fprintf(fid, 'Coefficient_Width = %d;\n', Bc);
|
||||
fprintf(fid, 'Coefdata =\n');
|
||||
for i = 1:NFIR
|
||||
if (i == NFIR)
|
||||
fprintf(fid, '%d;\n', hCOE(1,i));
|
||||
else
|
||||
fprintf(fid, '%d,\n', hCOE(1,i));
|
||||
end
|
||||
end
|
||||
fclose(fid);
|
||||
end
|
||||
|
||||
if (IS_HDR == 'Y')
|
||||
fid = fopen ('fir_filter.h', 'w');
|
||||
fprintf(fid, 'const int BL = %d;\n', NFIR);
|
||||
fprintf(fid, 'const int B[%d] = {\n', NFIR);
|
||||
for i = 1:NFIR
|
||||
if (i == NFIR)
|
||||
fprintf(fid, '%d}\n', hCOE(1,i));
|
||||
else
|
||||
fprintf(fid, '%d,\n', hCOE(1,i));
|
||||
end
|
||||
end
|
||||
fclose(fid);
|
||||
end
|
||||
|
1
Stuff/CICComp/tx.coe
Normal file
1
Stuff/CICComp/tx.coe
Normal file
@@ -0,0 +1 @@
|
||||
2.800554e+01,-2.710925e+01,1.967645e+01,-4.287149e+00,-1.957808e+01,5.117337e+01,-8.818982e+01,1.265920e+02,-1.606796e+02,1.833972e+02,-1.869879e+02,1.639282e+02,-1.081905e+02,1.662591e+01,1.095636e+02,-2.636392e+02,4.324937e+02,-5.966627e+02,7.311469e+02,-8.075047e+02,7.968760e+02,-6.742095e+02,4.228641e+02,-3.960783e+01,-4.613277e+02,1.045190e+03,-1.656290e+03,2.219916e+03,-2.648113e+03,2.848070e+03,-2.734149e+03,2.240892e+03,-1.337477e+03,3.957720e+01,1.580998e+03,-3.393695e+03,5.212197e+03,-6.807600e+03,7.927908e+03,-8.327317e+03,7.798005e+03,-6.206687e+03,3.525291e+03,1.419623e+02,-4.547706e+03,9.302038e+03,-1.389699e+04,1.774461e+04,-2.023907e+04,2.082478e+04,-1.907877e+04,1.478187e+04,-7.986836e+03,-9.465419e+02,1.133642e+04,-2.221429e+04,3.238679e+04,-4.055105e+04,4.542429e+04,-4.591047e+04,4.124944e+04,-3.117123e+04,1.598991e+04,3.331081e+03,-2.521126e+04,4.753079e+04,-6.781863e+04,8.347012e+04,-9.205268e+04,9.158788e+04,-8.086845e+04,5.967443e+04,-2.896177e+04,-9.123589e+03,5.129846e+04,-9.340721e+04,1.307395e+05,-1.585390e+05,1.724856e+05,-1.692906e+05,1.471358e+05,-1.061204e+05,4.841519e+04,2.165719e+04,-9.792523e+04,1.727483e+05,-2.377826e+05,2.847188e+05,-3.063104e+05,2.971731e+05,-2.547165e+05,1.796076e+05,-7.622019e+04,-4.758281e+04,1.807256e+05,-3.099434e+05,4.207489e+05,-4.990731e+05,5.325616e+05,-5.123526e+05,4.341113e+05,-2.993291e+05,1.154776e+05,1.036826e+05,-3.392237e+05,5.679024e+05,-7.645371e+05,9.037175e+05,-9.629548e+05,9.245141e+05,-7.785383e+05,5.240560e+05,-1.712618e+05,-2.591254e+05,7.348868e+05,-1.214970e+06,1.649588e+06,-1.984594e+06,2.161447e+06,-2.122105e+06,1.806062e+06,-1.152833e+06,8.873687e+04,1.483707e+06,-3.739433e+06,7.013816e+06,-1.203248e+07,1.932583e+07,1.932583e+07,-1.203248e+07,7.013816e+06,-3.739433e+06,1.483707e+06,8.873687e+04,-1.152833e+06,1.806062e+06,-2.122105e+06,2.161447e+06,-1.984594e+06,1.649588e+06,-1.214970e+06,7.348868e+05,-2.591254e+05,-1.712618e+05,5.240560e+05,-7.785383e+05,9.245141e+05,-9.629548e+05,9.037175e+05,-7.645371e+05,5.679024e+05,-3.392237e+05,1.036826e+05,1.154776e+05,-2.993291e+05,4.341113e+05,-5.123526e+05,5.325616e+05,-4.990731e+05,4.207489e+05,-3.099434e+05,1.807256e+05,-4.758281e+04,-7.622019e+04,1.796076e+05,-2.547165e+05,2.971731e+05,-3.063104e+05,2.847188e+05,-2.377826e+05,1.727483e+05,-9.792523e+04,2.165719e+04,4.841519e+04,-1.061204e+05,1.471358e+05,-1.692906e+05,1.724856e+05,-1.585390e+05,1.307395e+05,-9.340721e+04,5.129846e+04,-9.123589e+03,-2.896177e+04,5.967443e+04,-8.086845e+04,9.158788e+04,-9.205268e+04,8.347012e+04,-6.781863e+04,4.753079e+04,-2.521126e+04,3.331081e+03,1.598991e+04,-3.117123e+04,4.124944e+04,-4.591047e+04,4.542429e+04,-4.055105e+04,3.238679e+04,-2.221429e+04,1.133642e+04,-9.465419e+02,-7.986836e+03,1.478187e+04,-1.907877e+04,2.082478e+04,-2.023907e+04,1.774461e+04,-1.389699e+04,9.302038e+03,-4.547706e+03,1.419623e+02,3.525291e+03,-6.206687e+03,7.798005e+03,-8.327317e+03,7.927908e+03,-6.807600e+03,5.212197e+03,-3.393695e+03,1.580998e+03,3.957720e+01,-1.337477e+03,2.240892e+03,-2.734149e+03,2.848070e+03,-2.648113e+03,2.219916e+03,-1.656290e+03,1.045190e+03,-4.613277e+02,-3.960783e+01,4.228641e+02,-6.742095e+02,7.968760e+02,-8.075047e+02,7.311469e+02,-5.966627e+02,4.324937e+02,-2.636392e+02,1.095636e+02,1.662591e+01,-1.081905e+02,1.639282e+02,-1.869879e+02,1.833972e+02,-1.606796e+02,1.265920e+02,-8.818982e+01,5.117337e+01,-1.957808e+01,-4.287149e+00,1.967645e+01,-2.710925e+01,2.800554e+01
|
252
Stuff/CICComp/tx.m
Normal file
252
Stuff/CICComp/tx.m
Normal file
@@ -0,0 +1,252 @@
|
||||
%% ------------------------------------------------------------------------
|
||||
%
|
||||
% Title : test_cic.m
|
||||
% Author : Alexander Kapitanov
|
||||
% E-mail : sallador@bk.ru
|
||||
% Version : 1.0
|
||||
%
|
||||
% -------------------------------------------------------------------------
|
||||
%
|
||||
% Description :
|
||||
%
|
||||
% FIR filter compensator to correct freq response after CIC filter.
|
||||
%
|
||||
% -------------------------------------------------------------------------
|
||||
%
|
||||
% Version : 1.0
|
||||
% Date : 2017.06.03
|
||||
%
|
||||
%% ------------------------------------------------------------------------
|
||||
%
|
||||
% GNU GENERAL PUBLIC LICENSE
|
||||
% Version 3, 29 June 2007
|
||||
%
|
||||
% Copyright (c) 2018 Kapitanov Alexander
|
||||
%
|
||||
% This program is free software: you can redistribute it and/or modify
|
||||
% it under the terms of the GNU General Public License as published by
|
||||
% the Free Software Foundation, either version 3 of the License, or
|
||||
% (at your option) any later version.
|
||||
%
|
||||
% You should have received a copy of the GNU General Public License
|
||||
% along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
%
|
||||
% THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
% APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
% HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
% OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
% THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
% PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
% IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
% ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
%
|
||||
%% ------------------------------------------------------------------------
|
||||
|
||||
set(0, 'DefaultAxesFontSize', 11, 'DefaultAxesFontName', 'Times New Roman');
|
||||
set(0, 'DefaultTextFontSize', 11, 'DefaultTextFontName', 'Times New Roman');
|
||||
|
||||
close all;
|
||||
clear all;
|
||||
|
||||
%% ------------------------------------------------------------------------
|
||||
% ---- CIC Filter Parameters
|
||||
% ------------------------------------------------------------------------
|
||||
|
||||
R = 3920; % Decimation factor
|
||||
N = 3; % Number of stages
|
||||
M = 1; % Differential delay (only 1)
|
||||
|
||||
%% ------------------------------------------------------------------------
|
||||
% ---- FIR filter parameters
|
||||
% ------------------------------------------------------------------------
|
||||
|
||||
NFIR = 256; % Filter order, must be odd when Fo = 0.5 !!!
|
||||
Bc = 32; % Coef. Bit-width
|
||||
Fo = 0.45; % Normalized Cutoff: 0.2 < Fo < 0.5;
|
||||
BETA = 8; % BETA parameter for Kaiser window (if IS_WIND = 'Y')
|
||||
|
||||
%% ------------------------------------------------------------------------
|
||||
% ---- Save data parameters
|
||||
% ------------------------------------------------------------------------
|
||||
|
||||
IS_COE = 'Y'; % create *.COE Xilinx file
|
||||
IS_HDR = 'N'; % create *.H file (header)
|
||||
IS_WIND = 'Y'; % use Kaiser Window for FIR corrector
|
||||
|
||||
IS_PLOT_IDL = 'Y'; % plot ideal response
|
||||
IS_PLOT_FIR = 'Y'; % plot FIR filter IR
|
||||
IS_PLOT_ERR = 'Y'; % plot total error response in passband
|
||||
|
||||
%% ------------------------------------------------------------------------
|
||||
% ---- CIC Compensator Design
|
||||
% ------------------------------------------------------------------------
|
||||
|
||||
NFFT = 2^16; % FFT points for Freq Response (spectrum)
|
||||
STEP = 1/NFFT; % Step size
|
||||
w = -pi:2*pi/NFFT:pi-2*pi/NFFT;
|
||||
ff = 0:1/NFFT:1-1/NFFT;
|
||||
z = exp(1j * w);
|
||||
|
||||
Fc = 1/(2*R);
|
||||
Fr = Fo/R;
|
||||
|
||||
% 1 way:
|
||||
%HCIC = (1/R * (1-z.^(-R*M))./(1-z.^(-1))).^ N;
|
||||
% 2 way:
|
||||
HCIC = (R^-N*abs(1*M*sin(pi*M*R*ff) ./ sin(pi*ff)).^N);
|
||||
HCICdb = 20 * log10(abs(HCIC));
|
||||
|
||||
fp = [0:STEP:Fo]; % Pass band frequency dots
|
||||
fs = [(Fo+STEP):STEP:0.5]; % Stop band frequency dots
|
||||
f = [fp fs]*2; % Normalized frequency dots
|
||||
f(end) = 1;
|
||||
|
||||
% Calculate ideal response
|
||||
Mp = ones(1, length(fp)); % Pass band response; Mp(1) = 1
|
||||
Mp(2:end) = abs(M * R * sin(pi*fp(2:end)/R) ./ sin(pi*M*fp(2:end))).^(N);
|
||||
Mf = [Mp zeros(1, length(fs))];
|
||||
|
||||
if (IS_PLOT_IDL == 'Y')
|
||||
figure('name','FIR Ideal Response', 'Numbertitle', 'off')
|
||||
plot(f/2, Mf, '-.', 'LineWidth', 2, 'Color',[0 0 1]);
|
||||
|
||||
title([{'FIR Ideal Response'};{sprintf('Fo = %i',Fo)}]);
|
||||
xlabel ('Freq (\pi x rad / samples)');
|
||||
ylabel ('Magnitude');
|
||||
axis tight;
|
||||
legend([{sprintf('Fo = %i',Fo)}]);
|
||||
grid on;
|
||||
end
|
||||
|
||||
% Calculate FIR
|
||||
hFIR = fir2(NFIR-1, f, Mf); % Filter length NFIR
|
||||
hFIR = hFIR / max(hFIR); % Double coefficients
|
||||
hCOE = round(hFIR*(2^(Bc-1)-1)); % Fixed point coefficients
|
||||
|
||||
% Windowed FIR (Kaiser with BETA)
|
||||
if (IS_WIND == 'Y')
|
||||
WIND = kaiser(NFIR, BETA); % KAISER WINDOW IS USED!
|
||||
hWIND = fir1(NFIR-1, Fo/R, 'low', WIND);
|
||||
hNEW = hCOE .* hWIND;% conv2(hCOE,Hwind);
|
||||
hCOE = hNEW;
|
||||
end
|
||||
|
||||
if (IS_PLOT_FIR == 'Y')
|
||||
figure('name','FIR Response', 'Numbertitle', 'off')
|
||||
plot(hFIR, '-', 'LineWidth', 2, 'Color',[1 0 0]);
|
||||
|
||||
title([{'FIR Response'};{sprintf('Order = %i',NFIR)}]);
|
||||
xlabel ('Samples');
|
||||
ylabel ('Magnitude');
|
||||
axis tight;
|
||||
legend([{sprintf('Order = %i',NFIR)}]);
|
||||
grid on;
|
||||
end
|
||||
|
||||
hFFT = 20 * log10(abs(fft(hCOE, ceil(NFFT/R))));
|
||||
hFFT = hFFT - max(hFFT);
|
||||
|
||||
H_amp = repmat(hFFT, 1, R);
|
||||
H_amp = H_amp(1:length(ff));
|
||||
H_amp = H_amp - max(H_amp);
|
||||
|
||||
H_comp = HCICdb + H_amp;
|
||||
H_comp = H_comp - max(H_comp);
|
||||
|
||||
%% ------------------------------------------------------------------------
|
||||
% ---- Plot results
|
||||
% ------------------------------------------------------------------------
|
||||
|
||||
% ---- Figure #1
|
||||
figure('name','CIC/FIR Frequency Response', 'Numbertitle', 'off')
|
||||
plot(ff, HCICdb - max(HCICdb), '-.', 'LineWidth', 2, 'Color',[0 0 1]);
|
||||
hold on;
|
||||
|
||||
plot(ff, H_amp, '--', 'LineWidth', 2, 'Color',[0 0.4 0]);
|
||||
hold on;
|
||||
|
||||
plot(ff, H_comp, '-', 'LineWidth', 2, 'Color',[1 0 0]);
|
||||
hold on;
|
||||
|
||||
title([{'CIC, Comp. FIR and Result'};{sprintf('Filter Order = %i, Coef. width = %i',NFIR,Bc)}]);
|
||||
xlabel ('Freq (\pi x rad / samples)');
|
||||
ylabel ('Magnitude (dB)');
|
||||
axis([0 ff(NFFT)/2 -100 5]);
|
||||
line([Fr Fr], [-400 200], 'LineWidth', 1, 'linestyle', '--', 'Color', [0 0 0]);
|
||||
line([Fc Fc], [-400 200], 'LineWidth', 1, 'linestyle', '--', 'Color', [0 0 0]);
|
||||
line([2*Fc 2*Fc], [-400 200], 'LineWidth', 1, 'linestyle', '--', 'Color', [0 0 0]);
|
||||
legend('CIC filter','Comp. FIR','Sum Response','location','northeast');
|
||||
grid on;
|
||||
|
||||
% ---- Figure #2
|
||||
figure('name', 'CIC/FIR Frequency Response (Zoom)', 'Numbertitle', 'off')
|
||||
plot(ff, HCICdb - max(HCICdb), '-.', 'LineWidth', 2, 'Color',[0 0 1]);
|
||||
hold on;
|
||||
|
||||
plot(ff, H_amp, '--', 'LineWidth', 2, 'Color',[0 0.4 0]);
|
||||
hold on;
|
||||
|
||||
plot(ff, H_comp, '-', 'LineWidth', 2, 'Color',[1 0 0]);
|
||||
hold on;
|
||||
|
||||
title([{'CIC, Comp. FIR and Result'};{sprintf('Filter Order = %i, Coef. width = %i',NFIR,Bc)}]);
|
||||
xlabel ('Freq (\pi x rad / samples)');
|
||||
ylabel ('Magnitude (dB)');
|
||||
axis([0 ff(NFFT)/(2*R) -7 1]);
|
||||
line([Fr Fr], [-400 200], 'LineWidth', 1, 'linestyle', '--', 'Color', [0 0 0]);
|
||||
grid on;
|
||||
|
||||
%% ------------------------------------------------------------------------
|
||||
% ---- Passband irregularity
|
||||
% ------------------------------------------------------------------------
|
||||
if (IS_PLOT_ERR == 'Y')
|
||||
pass = ceil(0.85*length(H_comp)*Fr);
|
||||
stp = 0:(0.9*Fr)/pass:(0.9*Fr)-(0.9*Fr)/pass;
|
||||
stp = stp(2:pass);
|
||||
Irr = H_comp(2:pass);
|
||||
Ism = mean(Irr);
|
||||
Iav = (max(Irr)-min(Irr))/2;
|
||||
|
||||
figure('name', 'Passband irregularity', 'Numbertitle', 'off')
|
||||
plot(stp, Irr, '--', 'LineWidth', 2, 'Color',[0 0 1]);
|
||||
line([0 (0.9*Fr)], [Ism Ism], 'LineWidth', 2, 'linestyle', '-', 'Color', [0 0 0]);
|
||||
grid on;
|
||||
title([{'Passband irregularity'};{sprintf('Mean value = %f',Ism)};{sprintf('Freq error = %f (dB)',Iav)}]);
|
||||
xlabel ('Freq (\pi x rad / samples)');
|
||||
ylabel ('Magnitude (dB)');
|
||||
axis tight;
|
||||
%axis([0 ff(NFFT)/(2*R) -0.05 1]);
|
||||
end
|
||||
|
||||
%% ------------------------------------------------------------------------
|
||||
% ---- Save coe data to files
|
||||
% ------------------------------------------------------------------------
|
||||
if (IS_COE == 'Y')
|
||||
fid = fopen ('tx.coe', 'w');
|
||||
fprintf(fid, 'Radix = 10;\n');
|
||||
fprintf(fid, 'Coefficient_Width = %d;\n', Bc);
|
||||
fprintf(fid, 'Coefdata =\n');
|
||||
for i = 1:NFIR
|
||||
if (i == NFIR)
|
||||
fprintf(fid, '%d;\n', hCOE(1,i));
|
||||
else
|
||||
fprintf(fid, '%d,\n', hCOE(1,i));
|
||||
end
|
||||
end
|
||||
fclose(fid);
|
||||
end
|
||||
|
||||
if (IS_HDR == 'Y')
|
||||
fid = fopen ('fir_filter.h', 'w');
|
||||
fprintf(fid, 'const int BL = %d;\n', NFIR);
|
||||
fprintf(fid, 'const int B[%d] = {\n', NFIR);
|
||||
for i = 1:NFIR
|
||||
if (i == NFIR)
|
||||
fprintf(fid, '%d}\n', hCOE(1,i));
|
||||
else
|
||||
fprintf(fid, '%d,\n', hCOE(1,i));
|
||||
end
|
||||
end
|
||||
fclose(fid);
|
||||
end
|
||||
|
Reference in New Issue
Block a user