Prev: Question of Fourier Transoform of a transposed matrix? Thanks a lot.
Next: Two way communication through USB between MATLAB and PIC
From: Duc Duong on 26 Jul 2010 00:32 Hi, I have been struggling but still cannot understand why fftfilt() and conv() produce different filtered signals whose spectra are different. My case is: N = 154; % total sample points fs = 5.1655; % sampling frequency fp = 0.0258; % starting bandpass frequency as the percentage of Nyquist = fs/2 fc = 0.6453; % ending bandpass frequency as the percentage of Nyquist = fs/2 % create a filter coefficient b = fir1(N-1, [fp fc], 'bandpass', chebwin(N,100)); % Note: I also tested with a case of N/3 % compute by conv() s1 = conv(s, b); % retain middle part of s1 L = ceil(length(b)/2); s1 = s1(L:L+length(s)); % compute by fftfilt() % compute without fftshift(b); s2 = fftfilt(b, s); % compute with fftshift(b). I realize that if I call fftfilt() with fftshift(b), then the acquired signal is not shifted to the right. s3 = fftfilt(fftshift(b),s); Now, s1, s2, and s3 are different, so are their spectra. Please advise something may be wrong, also advise if I did wrong when call fftfilt with fftshift(b) to avoid the shifting. Thank you very much.
From: Wayne King on 26 Jul 2010 06:45
"Duc Duong" <dcduc80(a)gmail.com> wrote in message <i2j344$je2$1(a)fred.mathworks.com>... > Hi, > > I have been struggling but still cannot understand why fftfilt() and conv() produce different filtered signals whose spectra are different. > > My case is: > N = 154; % total sample points > fs = 5.1655; % sampling frequency > fp = 0.0258; % starting bandpass frequency as the percentage of Nyquist = fs/2 > fc = 0.6453; % ending bandpass frequency as the percentage of Nyquist = fs/2 > > % create a filter coefficient > b = fir1(N-1, [fp fc], 'bandpass', chebwin(N,100)); > % Note: I also tested with a case of N/3 > > % compute by conv() > s1 = conv(s, b); > % retain middle part of s1 > L = ceil(length(b)/2); > s1 = s1(L:L+length(s)); > > % compute by fftfilt() > % compute without fftshift(b); > s2 = fftfilt(b, s); > > % compute with fftshift(b). I realize that if I call fftfilt() with fftshift(b), then the acquired signal is not shifted to the right. > s3 = fftfilt(fftshift(b),s); > > Now, s1, s2, and s3 are different, so are their spectra. Please advise something may be wrong, also advise if I did wrong when call fftfilt with fftshift(b) to avoid the shifting. > > Thank you very much. > Hi Duc, Please look at the following: % creating a signal reset(RandStream.getDefaultStream) x = randn(1024,1); % Now implementing your filter N = 154; % total sample points fs = 5.1655; % sampling frequency fp = 0.0258; % starting bandpass frequency as the percentage of Nyquist = fs/2 fc = 0.6453; % ending bandpass frequency as the percentage of Nyquist = fs/2 % create a filter coefficient b = fir1(N-1, [fp fc], 'bandpass', chebwin(N,100)); y = filter(b,1,x); % Using filter() y1 = fftfilt(b,x); % using fftfilt y2 = conv(b,x,'full'); y2=y2(1:length(y1)); Now compare y,y1,and y2 norm(y-y1), norm(y-y2), norm(y2-y1) Hope that helps, Wayne |