-2
$\begingroup$

I had two sound files and did the following to it:

[s1, fs1, nbits1] = wavread('s1.wav');
[s2, fs2, nbits2] = wavread('s2.wav');
r1 = resample(s1, 22050, fs1);
r2 = resample(s2, 22050, fs2);
f1 = fft(r1);
f2 = fft(r2);
shift1 = fftshift(f1);
added = shift1 + f2;
out = ifft(added);
wavwrite(out, 22050, 'result.wav');

I can only hear the 's2.wav' when I play 'result.wav'. Then, I did this:

[s1, fs1, nbits1] = wavread('s1.wav');
[s2, fs2, nbits2] = wavread('s2.wav');
r1 = resample(s1, 22050, fs1);
r2 = resample(s2, 22050, fs2);
f1 = fft(r1);
f2 = fft(r2);
shift1 = fftshift(f1);
added = shift1 + f2;
shift2 = fftshift(added);
out = ifft(shift2);
wavwrite(out, 22050, 'result.wav');

This time, I could only hear the 's1.wav'. It seems strange to me. What is happening? The only thing I understood is that after I apply frequency shift, I get an imaginary part which did not exist before.

Can anyone explain me what is happening?

$\endgroup$
  • 3
    $\begingroup$ Your code makes little sense to me and I find it very hard to guess what you are trying to achieve. Can you explain what you believe the code does (or what it should do) and why you do it? $\endgroup$ – Jazzmaniac May 18 '15 at 12:04
  • $\begingroup$ @Jazzmaniac first code: I just did a frequency shift to s1, calculated s1+s2, and then saved it. second code: I did what I did for code 1 plus a final frequency shift to (s1+s2). $\endgroup$ – Nina May 18 '15 at 13:14
  • 1
    $\begingroup$ What the code is doing is fairly clear. What you think it is doing is an entirely different thing. Why are you doing the fftshift? What do you think it is doing? What result did you expect? $\endgroup$ – JRE May 18 '15 at 13:30
  • $\begingroup$ @JRE I did expected these results somehow. I just want to know their reasons. $\endgroup$ – Nina May 18 '15 at 15:49
  • $\begingroup$ It is likely that fftshift() turns a loud noise into a really acute noise. For instance, let's say a complex signal of frequency 100Hz is at index 50 and 22050-50. Its future index is 11025-50 and 11025+50 and the corresponding frequency is (11025-50)/50*100=22kHz. Either your speaker or your hear may act as a filter and the signal is lost. If you use a signal with high frequencies as input of fftshift(), you might be able to hear something... $\endgroup$ – francis May 18 '15 at 17:10
0
$\begingroup$

The fft function in matlab returns spectrum in the frequency range 0:Fs, most DSP engineers (and a lot of DSP tools) look at the FFT from -Fs/2 up to Fs/2. The fftshift function shifts the output from the fft into a symmetric form to put it into the latter form. It is often used for plotting the spectrum of a signal.

Some sample code is shown below

[s1 ,fs, xx] = wavread('./sound_files/cindy.wav');
lenfft = 1024;

fft1 = fft(s1,lenfft);
freq1 = [0:lenfft-1].*(fs/lenfft);
fft2 = fftshift(fft1);
freq2 = ceil(-(lenfft-1)/2:1:(lenfft-1)/2).*(fs/lenfft);


figure, 
subplot(2,1,1)
plot(freq1,abs(fft1))
title(['"stock" |FFT| F_s=' num2str(fs)])

subplot(2,1,2)
plot(freq2,abs(fft2))
title('"shifted" by fftshift |FFT|')

This produces the following output: example of shifting fft by fftshift.

The top plot show the spectrum with the low-frequency components at the bottom and the top of the spectrum. The lower plot has been fftshifted to put the low-frequency portions together in the middle of the plot. Explication on this phenomena can be found here.

So, what you are experiencing is that when you fftshift one of the signals (say s1) and then combine it with the other, you have then taken the low frequency portions and moved them to the high frequency part of the spectrum. So you resultant frequency spectrum will have the untouched signal of s2 and then s1 signal superimposed in the highest frequencies.

$\endgroup$
  • $\begingroup$ Thanks. However, why do I hear the sound that its component is NOT in the middle of the spectrum? $\endgroup$ – Nina May 18 '15 at 18:16
  • $\begingroup$ Do you get different results when you output real(out) variable versus the abs(out)? (I'm not sure how wavread takes into account complex values.) For me, when I shifted signal 1, then I could hear signal 2, and a really high-pitched, distorted signal 1. $\endgroup$ – aepound May 18 '15 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.