I prepared a Matlab script, which re-samples spectrum from 24kHz to 20kHz. When I used up-sampling, a fir filter, and then up-sampling again at the end, I get an additional peak at 4kHz which is not expected. When I checked via the resample function from Matlab paltette, there is no additional peak. What did I do wrong? What do I have to add to my script to avoid this kind of situation. Thank you in advance
clc;
close all;
clear variables;
Fs1 = 24000;
TimeStep1 = 1/Fs1;
Lenght = 10000;
time1 = (0:Lenght-1)*TimeStep1;
InputSignal = sin(2*pi*8000*time1);
NFFT1 = 2^nextpow2(Lenght);
Y1 = fft(InputSignal,NFFT1)/Lenght;
f1 = Fs1/2*linspace(0,1,NFFT1/2+1);
figure
plot(f1,2*abs(Y1(1:NFFT1/2+1)))
title('Original Spectrum')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
Fs2 = 20000;
T2 = 1/Fs2;
nGcd = gcd(Fs1, Fs2);
upY = upsample(InputSignal,Fs2/nGcd);
firCoeff = fir1(255, 0.9, 'low');
filteredY = filter(firCoeff,1,upY);
dataOut = resample(InputSignal,Fs2,ceil(Fs1));
L2 = length(dataOut);
t2 = (0:L2-1)*T2;
N=2;
lenght = (L2/N);
NFFT = 2^nextpow2(lenght);
Y = fft(dataOut,NFFT)/lenght;
f = Fs2/2*linspace(0,1,NFFT/2+1);
figure
plot(f,2*abs(Y(1:NFFT/2+1)))
title('Resampled Spectrum')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
figure
dataOut2 = downsample(filteredY,Fs1/nGcd);
Y2 = fft(dataOut2,NFFT)/lenght;
plot(f,2*abs(Y2(1:NFFT/2+1)))
title('UpFIRDwn Spectrum')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')