⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cic.m

📁 利用matlab
💻 M
字号:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CIC compensating filter design using frequency sampling method

clear all
close all

%%%%%% CIC filter parameters %%%%%%
R = 4;                                      %% Decimation factor
M = 2;                                      %% Differential Delay
N = 4;                                      %% Number of Stages
B = 17;                                     %% Number of bits to represent fixed point filter coefficients
Fs = 80e6;                              %% (High) Sampling frequency in Hz (before decimation)
Fc = 4e6;                                %% Passband edge in Hz
%%%%%%% fir2.m parameters %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
L = 30;                                    %% Order of filter taps; must be an even number
Fo = R*Fc/Fs;                               %% Normalized Cutoff freq; 0<Fo<=0.5/M; 
                                            %% Fo should be less than 1/(4M) for good performance                              
% Fo = 0.5/M;                                 %% use Fo=0.5 if we don't care responses outside passband

%%%%%%% CIC Compensator Design using fir2.m %%%%%%
p = 2e3;                                    %% Granulatiry
s = 0.25/p;                                 %% Stepsize
fp = [0:s:Fo];                              %% Passband frequency samples                                        
fs = (Fo+s):s:0.5;                          %% Stopband frequency samples
f = [fp fs]*2;                              %% Noramlized frequency samples; 0<=f<=1; 
Mp = ones(1,length(fp));                    %% Passband response; Mp(1)=1
Mp(2:end) = abs( M*R*sin(pi*fp(2:end)/R)./sin(pi*M*fp(2:end))).^N; %% Inverse sinc
Mf = [Mp zeros(1,length(fs))];
f(end) = 1;
h = fir2(L,f,Mf);                           %% Filter length L+1
h = h/norm(h);                              %% Floating point coefficients
hz = floor(h*power(2,B));                   %% Quantization of filter coefficients

%%%%%%% Full resolution CIC filter response %%%%%%%%
hrec = ones(1,R*M);
tmph = hrec;

for k=1:N-1
    tmph = conv(hrec, tmph);
end;
hcic = tmph;
hcic=hcic/norm(hcic);

%%%%%%% Total Response %%%%%%%%%%%%%%%
hzp = upsample(hz,R);
hp = upsample(h, R);
ht = conv(hcic, hp);                        %% Concatenation of CIC and fir2 FIR at high freqency
hzt = conv(hcic, hzp);                      %% CIC + Fixed point fir2 at high frequency

[Hcic, wt] = freqz(hcic, 1, 4096, Fs);      %% CIC Freq. Response
[Hciccomp, wt] = freqz(hp, 1, 4096, Fs);    %% CIC Comp. response using fir2
[Ht, wt] = freqz(ht, 1, 4096, Fs);          %% Total response for CIC + floating point fir2
[Hzt, wt] = freqz(hzt, 1, 4096, Fs);        %% Total response for CIC + fixed point fir2

Mcic = 20*log10(abs(Hcic)/max(abs(Hcic)));  %% CIC Freq. Response
Mciccomp = 20*log10(abs(Hciccomp)/max(abs(Hciccomp)));  %% CIC Comp. response using fir2
Mt = 20*log10(abs(Ht)/max(abs(Ht)));        %% Total response for CIC + floating point fir2
Mzt = 20*log10(abs(Hzt)/max(abs(Hzt)));     %% Total response for CIC + fixed point fir2

figure;
plot(wt, Mcic, wt, Mciccomp, wt, Mt,wt, Mzt);
legend('CIC','CIC Comp','Total Response (Floating Point)','Total Response (Fixed Point)')
ylim([-100 5]);
title('Frequency Sampling Method');
grid
xlabel('Frequency Hz');
ylabel('Filter Magnitude Response dB');

%%%%%%%%% Save filter coefficients for Altera FIR Compiler %%%%%%%%%
filename = ['fdcoeffR',num2str(R),'N',num2str(N),'M',num2str(M),'L',num2str(L),'.txt'];
fid = fopen(filename, 'wt');
fprintf(fid, '%18.0f \n', hz); %% fixed point coeff
% fprintf(fid, '%0.6d\n',h); %% floating point
fclose(fid)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -