📄 fir_top_mlab.m
字号:
%
%THIS IS A WIZARD GENERATED FILE. DO NOT EDIT THIS FILE!
%
%---------------------------------------------------------------------------------------------------------
%This is a filter with variable coefficients combine with interpolatipon and decimation
%Please input:
%data vector: stimulation(1:n)
%coefficient matrix: coef_matrix(n * interpolation_factor,m). n is the input data length, m is the coefficients length.
% coef_matrix(1,:) coressponds to coefficient at time unit 1.
% coef_matrix(2,:) coressponds to coefficient at time unit 2.
%
% coef_matrix(n,:) coressponds to coefficient at time unit n.
%
%FILTER PARAMETER
%Input Data Type: Signed
%Input Data Width: 12
%MSB Truncation Bits: 0
%MSB Truncated
%MSB Satuated
%LSB Truncation bits: 0
%LSB Truncated
%LSB Rounded
%OUTPUT Data Width: 27
%-----------------------------------------------------------------------------------------------------------
%MegaWizard Scaled Coefficient Values
function output = fir_top_st_mat (stimulation, output)
coef_matrix=[363 377 391 405 418 430 441 452 461 471 479 486 493 497 502 506 508 510 511 510 508 506 502 497 493 486 479 471 461 452 441 430 418 405 391 377 363 ];
INTER_FACTOR = 1;
DECI_FACTOR = 1 ;
MSB_RM = 0;
MSB_TYPE = 0;
LSB_RM = 0;
LSB_TYPE = 0;
FIR_WIDTH = 27;
OUT_WIDTH = FIR_WIDTH - MSB_RM - LSB_RM;
DATA_WIDTH= 12;
data_type= 1;
% check size of inputs.
[DX,DY] = size(stimulation);
[CX,CY] = size(coef_matrix);
%fill coef_matrix to length of data with the latest coef set
if (CX < DY * INTER_FACTOR)
for i= CX +1:DY * INTER_FACTOR
coef_matrix(i,:) = coef_matrix(CX,:);
end
end
%check if input is integer
int_sti=round(stimulation);
T = (int_sti ~= stimulation);
if (max(T)~=0)
fprintf('WARNING : Integer Input Expected: Rounding Fractional Input to Nearest Integer...\n');
end
%Input overflow check
switch data_type
case 1
%set max/min for signed
max = 2^(DATA_WIDTH-1)-1;
min = -max-1;
case 2
%set max/min for unsigned
max = 2^DATA_WIDTH-1;
min = 0;
end
%Saturating Input Value
a=find(int_sti>max);
b=find(int_sti<min);
if (~isempty(a)|~isempty(b))
fprintf('WARNING : Input Amplitude Exceeds MAXIMUM/MINIMUM allowable values - saturating input values...\n');
lena = length (a);
lenb = length (b);
for i =1:lena
fprintf('%d > %d \n', int_sti(a(i)), max);
int_sti(a(i)) = max;
end
for i =1:lenb
fprintf('%d < %d \n', int_sti(b(i)), min);
int_sti(b(i)) = min;
end
end
% Add interpolation
inter_sti = zeros(1, INTER_FACTOR * length(int_sti));
inter_sti(1:INTER_FACTOR:INTER_FACTOR * length(int_sti)) = int_sti;
for i = 1 : DY *INTER_FACTOR
coef_current = coef_matrix(i,:);
output_temp(i) = simp_adaptive (inter_sti, coef_current, i);
end
% Truncate output
len1 = length(output_temp);
switch LSB_TYPE
case 0
%truncate
out_dec = bi_trunc_lsb(output_temp,LSB_RM,FIR_WIDTH);
case 1
%round
out_dec = bi_round(output_temp,LSB_RM, FIR_WIDTH);
end
switch MSB_TYPE
case 0
%truncate
out_dec = bi_trunc_msb(out_dec,MSB_RM,FIR_WIDTH-LSB_RM);
case 1
%round
out_dec = bi_satu(out_dec,MSB_RM, FIR_WIDTH-LSB_RM);
end
% choose decimation output in phase=DECI_FACTOR-1
if(DECI_FACTOR == 1)
output = out_dec;
else
output = out_dec(DECI_FACTOR:DECI_FACTOR:len1);
end
function[output, outindex] = simp_adaptive (int_sti, coef_current, data_index, output)
%Simulation is the whole input sequence
%coef_current is the current coefficient set
%data_index gives the last data to use
%outputs are the sum of input and coef multiplication
%outindex is the next data_index
sti_current = zeros(length(coef_current),1);
data_length = length(int_sti);
%Check data index
if (data_index > data_length)
fprintf('ERROR: DATA INDEX IS LARGER THAN DATA LENGTH!!!\n');
break;
end
for i = 1: length(coef_current)
if ((data_index -i+1)>0 & (data_index - i+1)<=data_length)
sti_current(i,1) = int_sti(data_index - i+1);
end
end
outindex= data_index+1;
output = coef_current * sti_current;
% end of function simp_adaptive
function output = bi_round(data_in,LSB_RM,ORI_WIDTH, output)
% LSB_RM is the bit to lose in LSB
% ORI_WIDTH is the original data width
data = round (data_in / 2^LSB_RM);
output = bi_satu(data,0,ORI_WIDTH - LSB_RM);
%end of function bi_trunc_lsb
function output = bi_trunc_lsb(data_in,LSB_RM,ORI_WIDTH, output)
% LSB_RM is the bit to lose in LSB
% ORI_WIDTH is the original data width
%2's complement system
output = bitshift(2^ORI_WIDTH*(data_in<0) + data_in, -LSB_RM) - 2^(ORI_WIDTH-LSB_RM) *(data_in<0);
% end of function bi_round
function output = bi_trunc_msb(data_in,MSB_RM,ORI_WIDTH, output)
% MSB_RM is the bit to lose in LSB
% ORI_WIDTH is the original data width
%2's complement system
data = 2^ORI_WIDTH * (data_in < 0)+ data_in;
erase_num = 2^(ORI_WIDTH - MSB_RM) - 1;
data = bitand(data, erase_num);
output = data - 2^(ORI_WIDTH - MSB_RM)*(bitget(data,ORI_WIDTH - MSB_RM));
%end of bi_trunc_msb
function output = bi_satu(data_in,MSB_RM,ORI_WIDTH, output)
% MSB_RM is the bit to lose in LSB
% ORI_WIDTH is the original data width
%2's complement system
max = 2^(ORI_WIDTH - MSB_RM -1)-1;
min = 2^(ORI_WIDTH - MSB_RM -1)*(-1);
data_in(find(data_in > max)) = max;
data_in(find(data_in < min)) = min;
output = data_in;
%end of bi_satu
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -