MATLAB: Extract a thin band of frequencies from entire spectrum using FFT

fftfrequencies

Hello,
I have following program:
fs=1e4;
x=randn(1,1e4); %gaussian dist random signal
x_fft=fft(x); %fft of the signal
N=length(x);
omega=fs*[(0:N/2) (-N/2+1:-1)]/N; %frequency axis(bins)
figure;
plot(omega(1:N/2),2*abs(x_fft(1:N/2))); %plot of one-sided spectrum
Now I want to extract only a thin band of frequencies(say 500-520Hz) from the spectrum,take its ifft store the obtained time-domain signal in a variable y. I know it is bandpass filtering in TD and there is function fdesign.bandpass() in matlab aswell for this purpose, but I want to do it in frequency domain (using FFT and IFFT) as in my case, the samples are very large and BPF is very time consuming.
At the end, I want to have a small function, where I only enter the frequency band of interest as my input and I get the corresponding TD signal extracted as my output.
Please help.

Best Answer

fs=1e4;pas=1/fs
x=randn(1,1e4); %gaussian dist random signal
x_fft=fft(x); %fft of the signal
N=length(x);
[wc,w0,a0,ak,bk,c0,ck]=get_harmonics(x_fft,pas,2)
fc=wc/(2*pi),
%to extract index frequencies from ck
idx=find(fc>=500 & fc<=520)
% corresponding temporel signal signal(t)
t=0:0.1:10000;
signal=zeros(1,numel(t));
for l=1:length(idx)
k=idx(l);
signal=signal+ck(k)*exp(j*k*w0*t);
end
plot(t,real(signal)); %more t is near inf more imag(signal) is near 0.