Clear Filters
Clear Filters

Delay using FFT

23 views (last 30 days)
zozo
zozo on 23 Feb 2012
Answered: Ranim Tom on 18 Dec 2021
I have the following code to acurately implement time delay in any signal(wideband) in frequency domain by using the efficiency of FFT:
%****code edited (last 5 lines)****%
clc
clear all
close all
fs=10000;
f1=700;
t_duration=1;
t = 0:1/fs:t_duration-1/fs;
s = sin(2*pi*f1*t)+cos(8*pi*f1*t); %input signal
slen=length(s);
s=s';
[rn,c] = size(s);
d=0.000553239; %time delay in seconds
nfft = 2^nextpow2(2*slen); % To use max. computational efficiency of FFT
fax = fs*(-nfft/2:nfft/2-1)'/nfft; % Create frequency shift vectors(bins)
shft = exp(-j*d*2*pi*fax); % w=2*pi*fax,, Frequency function for delay
shft = ifftshift(shft); % Make axis compatable with numeric FFT
fsd = fft(s(:,1),nfft); % Take FFT
fsd = fsd.*shft; % Apply delay
dum = ifft(fsd); % Return to time domain
sd(:,1) = real(dum(1:slen)); % Trim time domain signal to required length
s1=sin(2*pi*f1*(t-d))+cos(8*pi*f1*(t-d));
plot(s1);
hold on
plot(sd,'y');
legend('mathematically delayed','delayed in frequency domain');
diff=sd'-s1; %difference signal
stem(diff);
*****question updated******
However, after carefully observing the plots obtained, I have following doubt:
I still see a problem area that is, when I compare the output 'sd' of the above algorithm with 's1'(*5 lines added at the end of code*), they should be same, but there is difference as observed in plot(diff). why? and how can I bring this difference close to zero?
Please guide.
Thank you.

Accepted Answer

Honglei Chen
Honglei Chen on 23 Feb 2012
I don't see anything obviously wrong in your code. I think the difference in the amplitude may just due to the fact that after the signal gets delayed, it gets sampled at a different location.
I also don't see your issue of 2 sample. Remember you are still sampling using the same frequency so there is no 1.6597 sample.
To make sure the things are working properly, I would suggest you to use an "easier" pair of fs and f1, i.e., make fs an integer multiply of f1. Thus you have same number of samples for each period and is easier to track. BTW, in your example, you will have aliasing as the second frequency component in s is 2800 Hz, which cannot be sufficiently sampled by 3000 Hz. This being said, it should not affect your result because the algorithm doesn't really are whether the signal has aliasing.
  1 Comment
zozo
zozo on 26 Feb 2012
I still see a problem area that is, when I compare the output 'sd' of the above algorithm with 's1'(**5 lines added at the end of code**), they should be same, but there is difference as observed in plot(diff).
why? and how can I bring this difference close to zero?
(refer updated code above)

Sign in to comment.

More Answers (1)

Ranim Tom
Ranim Tom on 18 Dec 2021
Hello @zozo, sorry for the long reply.
As answer to your question: the error is due because the algorithm mathematically dealyed is technically wrong I think to compute the delay that way.
Explanation: Althought that is drue a delay operation for some signal x(t) is expressed as x(t - d), and you can directly type in matlab, but remember that x(t - d) means that the signal x(t) start after d seconds, and anything before d seconds is 0.
In matlab, when you type for example s1=sin(2*pi*f1*(t-d))+cos(8*pi*f1*(t-d)) : t - d will give a vector that starts even before 0 (give negative time values), because matlab will subtract from each value of the t vector that value d (for example 0 - 0.011 s = -0.011 s, and so on...) , and hence will compute s1 for all these values (before d seconds and even before 0)
So again altough it is naturally to think directly typing x(t - d), however in an implementation it is wrong.
I hope I answered your question.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!