FormatSpec = '%d';

N = 3;
R = 1;
B = 14;
M = 3;

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;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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);

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:1/PointsNum*R:1-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(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(ImpulseResp)
grid on;
grid minor;
title('In Signal')
xlabel('Time')
ylabel('Amp')

subplot(2,1,2)
plot(ImpuseRespFftDb)
grid on;
grid minor;
title('Single-Sided Amplitude Spectrum of InData')
xlabel('f (Hz)')
ylabel('Amp')






























