# How does it go about implementing pitch shifting without affecting the duration or speed on matlab? [closed]

I am creating a project that will record a sound sample from the world, for example, a bird's chirping. And then through pitch scaling, I could create different notes.

I first tried to change the sampling rate fs below in the Matlab code, and I noticed that it will change both the duration and the pitch. After some research, i found out this method is called sample rate conversion. but I still do not quite understand why with a higher sampling rate, the octave created will sound slower and lower, and vice versa.

It would be really helpful if someone could provide some insight into this. Thank you in advance!

function [x1, x2, x3, x4, x5, x6, x7] = signal_generator(n1,n2,n3,n4,n5,n6,n7,fs)
% n is the signal frequency, fs is the sampling frequency

m = fs; % how many samples per second
t = 0:(m-1);
x1 = sin(2*pi*n1/fs*t);
x2 = sin(2*pi*n2/fs*t);
x3 = sin(2*pi*n3/fs*t);
x4 = sin(2*pi*n4/fs*t);
x5 = sin(2*pi*n5/fs*t);
x6 = sin(2*pi*n6/fs*t);
x7 = sin(2*pi*n7/fs*t);

end

• The way the question is phrased makes it too broad. The broad name of the techniques you are looking to experiment with is Pitch Shifting. Speeding up or slowing down the recording is part of Pitch Shifting but to do it without distortions you need a little bit more. Do you think you might be able to make the question more specific after dong some preliminary reading on the subject? – A_A Feb 26 '18 at 8:34
• OK i will read about it today and rephrase the question by tonight. – Yihan Hu Feb 26 '18 at 15:31
• As I brought up the sampling frequency fs, the octave sounded lower and slower; brought down, sounded higher and faster. But I really don't understand why. Imagine a vinyl record on a turntable, turned by hand. If you turn it faster, the pitch goes up and the song plays faster. If you turn it more slowly, the pitch goes down and the song plays more slowly. That's (more or less) the same thing you're observing here. – Guest Feb 26 '18 at 16:47
• @Guest the observation is really intuitive but it does not really explain why with a higher sampling frequency rate, the sample sounds slower and lower. Thank you for your help tho – Yihan Hu Feb 26 '18 at 20:11
• @YihanHu, you're dividing by the sampling frequency fs, so as fs increases, the result decreases. To extend the record example, records are recorded at two different speeds, 33rpm and 45rpm. Think of that recording speed as the sampling frequency. Now say your turntable is set to 33rpm. When you play a song recorded at 33rpm, it sounds normal. When you play the same song recorded at 45rpm (increased fs), with your turntable still set at 33rpm, the song plays back plays slower, not faster. – Guest Feb 26 '18 at 20:29

## 1 Answer

For the speed change: Sample rate is the rate at which samples are read right? So, as per that definition, since you have a higher sample rate, you would be reading more samples in one second. The length of the file remains the same but now since the samples are read faster, the overall length of the file would become slower (with an increase in sampling frequency).

For the pitch change: Now, let us consider the case of increasing the sampling frequency. I'm sorry for giving a rough explanation for this but imagine increasing the sampling frequency would squish the signal in the time axis since now lesser samples represent the same length in samples. This would mean an increase in frequency, thus resulting in a pitch shift.