MATLAB: Changing frequency of .wav

change frequencyfourierfrequency

Hi,
I'm experimenting with changing the frequency of an imported .wav file. I've looked on the mathworks fora and haven't found a working solution. Someone pointed out (in 2005) that an easy solution of changing the frequency is to use Hilbert transformation. Well, i tried, the code worked, but the 'frequency shifting' didn't work as flawless as i'd hoped. What is want to achieve is to import for example an 'A-tone' of frequency 220 Hz and change the tone to 'C-note' of frequency 261.6 Hz. I used the FFT() function to get the frequency of the .wav file. I used a piano sound of frequency 440Hz and didn't succeed at changing the frequency of the audio vector. Does anyone know how to do this in a proper way? Any information relating to changing the frequency is much appreciated.
Thanks for the help!

Best Answer

One way to do what I believe you want to do is to heterodyne your initial signal with the difference between it and the the new frequency. This is called ‘amplitude modulation’, and specifically here ‘double sideband suppressed carrier’ modulation. I then filter out the lower sideband to produce a ‘single-sideband suppressed carrier’ signal at the desired (upper sideband) frequency.
Don’t worry about the details. I’m simply explaining what my code does and how it works. My code requires the Signal Processing Toolbox to design and implement the filter.
The modulation technique multiplies two sinusoids of different frequencies together to produce sinusiods with the sum and difference of those frequencies. The mathmatical background is given in the Wikipedia entry on Product-to-sum and sum-to-product identities.
The code:
Fs = 4.41E4; % Sampling Frequency (Hz)
tmax = 5; % Time Duration Of Signal (sec)
t = linspace(0, tmax, tmax*Fs); % Time Vector
f = 220; % Original Frequency
s = sin(2*pi*f*t + cos(15*pi*t)*pi/2); % Original Signal
sound(s, Fs) % Listen To Original Signal
pause(tmax*2) % Wait For It To Finish
Fc = 261.6; % Desired Output Frequency
carrier = sin(2*pi*(Fc-f)*t); % Generate Carrier
sm = s .* carrier; % Modulate (Produces Upper & Lower Sidebands
Fn = Fs/2; % Design High-Pass Filter To Eliminate Lower Sideband
Wp = Fc/Fn;
Ws = Wp*0.8;
[n,Wn] = buttord(Wp, Ws, 1, 10);
[b,a] = butter(n,Wn,'high');
[sos,g] = tf2sos(b,a);
smf = 2*filtfilt(sos,g,sm); % Output Is Suppressed-Carrier Upper Sideband Signal
sound(smf, Fs) % Listen To New Signal
Fn1 = Fs/2; % Compute & Plot Fourier Series Of Both
Ft1 = fft(s)/length(s);
Fv1 = linspace(0, 1, fix(length(Ft1)/2)+1)*Fn1;
Ix1 = 1:length(Fv1);
Fn2 = Fs/2;
Ft2 = fft(smf)/length(smf);
Fv2 = linspace(0, 1, fix(length(Ft2)/2)+1)*Fn2;
Ix2 = 1:length(Fv2);
figure(1)
plot(Fv1, abs(Ft1(Ix1)))
grid
hold on
plot(Fv2, abs(Ft2(Ix2)))
hold off
axis([0 500 ylim])