FormatSpec = '%d';

N = 4;
R = 2;
B = 14;
M = 1;

PointsNum = 100;

Bmax = ceil(log2(((R*M)^N)/R)+B);
MaxWidthR2 = ceil(log2(((2*M)^N)/2)+B);
MaxWidthR4 = ceil(log2(((4*M)^N)/4)+B);

K = N*20*log10(M);
BitGrowth = ceil(2^(K/20));
Bnew = B+BitGrowth;

x = 1:1:PointsNum;
xDecim = 1:1:PointsNum/R;

Fc = 3;
Fs = 50;

DeltaPulse = [1, zeros(1, PointsNum*R-1)];
DeltaPulseTransposed = transpose(DeltaPulse);

IdealCicDecim = dsp.CICDecimator(R,M,N);
ImpResp = IdealCicDecim(DeltaPulseTransposed);

FftPNum = 50000;
Step = 1 / FftPNum;
f = linspace(1e-10, 1 - Step, FftPNum);
H = ones(1,50000);

for i = 1:50000
    H(i) = abs(sin(pi * M * R * f(i)) / sin(pi * f(i)))^N;
end

Hdb = 20 * log10(abs(H));
Hdb = Hdb - max(Hdb);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ReadInDataId = fopen('C:/S5243_FFT_REPO/src/src/Sim/InputSignal.txt','r');
InDataSignal = fscanf(ReadInDataId,FormatSpec);
fclose(ReadInDataId);

ReadFilteredDataId = fopen('C:/S5243_FFT_REPO/src/src/Sim/FilteredData.txt','r');
FilteredData = fscanf(ReadFilteredDataId,FormatSpec);
fclose(ReadFilteredDataId);

ReadImpulseRestId = fopen('C:\S5243_FFT_REPO\src\src\Sim\ImpResp.txt','r');
ImpulseResp = fscanf(ReadImpulseRestId,FormatSpec);
fclose(ReadImpulseRestId);

ReadFilterCoefsId = fopen('C:\S5243_FFT_REPO\src\src\Sim\CorrFilterCoefs.txt','r');
FilterCoefs = fscanf(ReadFilterCoefsId,FormatSpec);
fclose(ReadFilterCoefsId);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

FreqBandOrig = Fs*(1:(PointsNum))/PointsNum;
FreqbandDecim = (Fs/2*R)*(1:(PointsNum))/PointsNum;

InDataFft = abs(fft(InDataSignal+randn(size(InDataSignal)))/PointsNum);
InDataFftDb = 20*log10(InDataFft);

IdealCicFFt = abs(fft(ImpResp));
IdealCicFFtDb = 20*log10(IdealCicFFt);
IdealCicFFtDb = IdealCicFFtDb-max(IdealCicFFtDb);

CalcCicFft = abs(fft(ImpResp));
CalcCicFftDb = 20*log10(IdealCicFFt);
CalcCicFftDb = IdealCicFFtDb-max(IdealCicFFtDb);

FilteredDataFft = abs(fft(FilteredData+randn(size(FilteredData)))/PointsNum);
% FilteredDataFft = abs(fft(FilteredData)/PointsNum);
FilteredDataFftDb = 20*log10(FilteredDataFft);
FilteredDataFftDb = FilteredDataFftDb-max(FilteredDataFftDb);

ImpuseRespFft = abs(fft(ImpulseResp));
ImpuseRespFftDb = 20*log10(ImpuseRespFft);
ImpuseRespFftDb = ImpuseRespFftDb-max(ImpuseRespFftDb);

Fband = 0:(Fs/R)/(PointsNum/R):(Fs/R)-1/(PointsNum/R);
FbandOrig = 0:Fs/PointsNum:Fs-1/PointsNum;

%InDataFftDb = InDataFftDb(1:length(Fband));
%FilteredDataFftDb = FilteredDataFftDb(1:length(Fband));
%ImpuseRespFftDb = ImpuseRespFftDb(1:length(Fband));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% figure('name','In Data Time/Freq', 'Numbertitle', 'off')
% subplot(2,1,1)
% plot(InDataSignal)
% grid on;
% grid minor;
% title('In Signal')
% xlabel('Time')
% ylabel('Amp')
% 
% subplot(2,1,2)
% plot(FilteredData)
% grid on;
% grid minor;
% title('Filtered Data')
% xlabel('Time')
% ylabel('Amp')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% figure('name','Frequency compare', 'Numbertitle', 'off')
% subplot(3,1,1)
% plot(FbandOrig,InDataFftDb)
% grid on;
% grid minor;
% title('Single-Sided Amplitude Spectrum of Data before filter')
% xlabel('Time')
% ylabel('Amp')
% 
% subplot(3,1,2)
% plot(FbandOrig,ImpuseRespFftDb)
% grid on;
% grid minor;
% title('Cic Frequency response')
% xlabel('f (Hz)')
% ylabel('Amp')
% 
% subplot(3,1,3)
% plot(Fband,FilteredDataFftDb)
% grid on;
% grid minor;
% title('Single-Sided Amplitude Spectrum of Data after filter')
% xlabel('f (Hz)')
% ylabel('Amp')
% 
% figure('name','Impulse Response Time/Freq', 'Numbertitle', 'off')
% subplot(2,1,1)
% plot(FbandOrig,ImpulseResp)
% grid on;
% grid minor;
% title('In Signal')
% xlabel('Time')
% ylabel('Amp')
% 
% subplot(2,1,2)
% plot(FbandOrig,ImpuseRespFftDb)
% grid on;
% grid minor;
% title('Single-Sided Amplitude Spectrum of InData')
% xlabel('f (Hz)')
% ylabel('Amp')


figure('name','In Data Time/Freq', 'Numbertitle', 'off')
plot(Hdb)
grid on;
grid minor;
title('In Signal')
xlabel('Time')
ylabel('Amp')

% figure(2)
% plot(IdealCicFFtDb)
% grid on;
% grid minor;
% title('Filtered Data')
% xlabel('Time')
% ylabel('Amp')






























