duge007
duge007

Reputation: 1

Shifting a signal to a higher frequency range

I want to shift an audio file from an audible frequency band to one that is higher than 20kHz using python. I have been searching online on how to do this and the closest i have got is using fft.fftshift.

The code is as follows:

samplerate, data = wavfile.read('./aud/vader.wav')
fft_out = fft(data)
shifted = np.fft.fftshift(fft_out)

I want 'shifted' to be the shifted version of fft_out. It should be shifted by a magnitude of 20kHz

Upvotes: 0

Views: 2490

Answers (1)

Heinz M.
Heinz M.

Reputation: 694

fftshift doesn't shift the original 20kHz frequency band to a higher band.

As you can read in the manual:

numpy.fft.fftshift¶ ... Shift the zero-frequency component to the center of the spectrum. This function swaps half-spaces for all axes listed

If you want to shift the band 0Hz-20kHz for example to the band 100kHz - 120kHz, you can multiply the audio signal with a cosinus function with frequency 100kHz. This produces the two bands 80kHz-100kHz and 100kHz-120 kHz. From the resulting spectrum you have to delete the lower band 80kHz-100kHz. You have to do this for the positive frequencies as well as for the negative frequencies, which are mirrored on the zero axis.

Why does this work?

cos(x) * cos(y) = 0.5 * ( cos(x+y) + cos(x-y) )

if x are the frequencies of your audio band and y is the shifting frequency of 100kHz you get:

cos(2pi*audiofrequency) * cos(2pi*100kHz) = 0.5 * (cos(2pi*(100kHz + audiofrequency)) + (cos(2pi*(100kHz - audiofrequency)) )

Upvotes: 2

Related Questions