main content

shift audio pitch -凯发k8网页登录

shift audio pitch

since r2019b

description

example

audioout = shiftpitch(audioin,nsemitones) shifts the pitch of the audio input by the specified number of semitones, nsemitones.

example

audioout = shiftpitch(audioin,nsemitones,name,value) specifies options using one or more name,value pair arguments.

examples

read in an audio file and listen to it.

[audioin,fs] = audioread('counting-16-44p1-mono-15secs.wav');
sound(audioin,fs)

increase the pitch by 3 semitones and listen to the result.

nsemitones = 3;
audioout = shiftpitch(audioin,nsemitones);
sound(audioout,fs)

decrease the pitch of the original audio by 3 semitones and listen to the result.

nsemitones = -3;
audioout = shiftpitch(audioin,nsemitones);
sound(audioout,fs)

read in an audio file and listen to it.

[audioin,fs] = audioread("speechdft-16-8-mono-5secs.wav");
sound(audioin,fs)

convert the audio signal to a time-frequency representation using . use a 512-point with 75% overlap.

win = kbdwin(512);
overlaplength = 0.75*numel(win);
s = stft(audioin, ...
    "window",win, ...
    "overlaplength",overlaplength, ...
    "centered",false);

increase the pitch by 8 semitones and listen to the result. specify the window and overlap length you used to compute the stft.

nsemitones = 8;
lockphase = false;
audioout = shiftpitch(s,nsemitones, ...
                     "window",win, ...
                     "overlaplength",overlaplength, ...
                     "lockphase",lockphase);
sound(audioout,fs)

decrease the pitch of the original audio by 8 semitones and listen to the result. specify the window and overlap length you used to compute the stft.

nsemitones = -8;
lockphase = false;
audioout = shiftpitch(s,nsemitones, ...
                     "window",win, ...
                     "overlaplength",overlaplength, ...
                     "lockphase",lockphase);
sound(audioout,fs)

read in an audio file and listen to it.

[audioin,fs] = audioread('femalespeech-16-8-mono-3secs.wav');
sound(audioin,fs)

increase the pitch by 6 semitones and listen to the result.

nsemitones = 6;
lockphase = false;
audioout = shiftpitch(audioin,nsemitones, ...
                     'lockphase',lockphase);
sound(audioout,fs)

to increase fidelity, set lockphase to true. apply pitch shifting, and listen to the results.

lockphase = true;
audioout = shiftpitch(audioin,nsemitones, ...
                     'lockphase',lockphase);
sound(audioout,fs)

read in the first 11.5 seconds of an audio file and listen to it.

[audioin,fs] = audioread('rainbow-16-8-mono-114secs.wav',[1,8e3*11.5]);
sound(audioin,fs)

increase the pitch by 4 semitones and apply phase locking. listen to the results. the resulting audio has a "chipmunk effect" that sounds unnatural.

nsemitones = 4;
lockphase = true;
audioout = shiftpitch(audioin,nsemitones, ...
    "lockphase",lockphase);
sound(audioout,fs)

to increase fidelity, set preserveformants to true. use the default cepstral order of 30. listen to the result.

cepstralorder = 30;
audioout = shiftpitch(audioin,nsemitones, ...
    "lockphase",lockphase, ...
    "preserveformants",true, ...
    "cepstralorder",cepstralorder);
sound(audioout,fs)

input arguments

input signal, specified as a column vector, matrix, or 3-d array. how the function interprets audioin depends on the complexity of audioin:

  • if audioin is real, audioin is interpreted as a time-domain signal. in this case, audioin must be a column vector or matrix. columns are interpreted as individual channels.

  • if audioin is complex, audioin is interpreted as a frequency-domain signal. in this case, audioin must be an l-by-m-by-n array, where l is the fft length, m is the number of individual spectra, and n is the number of channels.

data types: single | double
complex number support: yes

number of semitones to shift the audio by, specified as a real scalar.

the range of nsemitones depends on the window length (numel(window)) and the overlap length (overlaplength):

-12*log2(numel(window)-overlaplength)nsemitones-12*log2((numel(window)-overlaplength)/numel(window))

data types: single | double

name-value arguments

specify optional pairs of arguments as name1=value1,...,namen=valuen, where name is the argument name and value is the corresponding value. name-value arguments must appear after other arguments, but the order of the pairs does not matter.

before r2021a, use commas to separate each name and value, and enclose name in quotes.

example: 'window',kbdwin(512)

window applied in the time domain, specified as the comma-separated pair consisting of 'window' and a real vector. the number of elements in the vector must be in the range [1, size(audioin,1)]. the number of elements in the vector must also be greater than overlaplength.

note

if using shiftpitch with frequency-domain input, you must specify window as the same window used to transform audioin to the frequency domain.

data types: single | double

number of samples overlapped between adjacent windows, specified as the comma-separated pair consisting of 'overlaplength' and an integer in the range [0, numel(window)).

note

if using shiftpitch with frequency-domain input, you must specify overlaplength as the same overlap length used to transform audioin to a time-frequency representation.

data types: single | double

apply identity phase locking, specified as the comma-separated pair consisting of 'lockphase' and false or true.

data types: logical

preserves formants, specified as the comma-separated pair consisting of 'preserveformants' and true or false. formant preservation is attempted using spectral envelope estimation with cepstral analysis.

data types: logical

cepstral order used for formant preservation, specified as the comma-separated pair consisting of 'cepstralorder' and a nonnegative integer.

dependencies

to enable this name-value pair argument, set preserveformants to true.

data types: single | double

output arguments

pitch-shifted audio, returned as a column vector or matrix of independent channels.

algorithms

to apply pitch shifting, shiftpitch modifies the time-scale of audio using a phase vocoder and then resamples the modified audio. the time scale modification algorithm is based on [1] and [2] and is implemented as in stretchaudio.

after time-scale modification, shiftpitch performs sample rate conversion using an interpolation factor equal to the analysis hop length and a decimation factor equal to the synthesis hop length. the interpolation and decimation factors of the resampling stage are selected as follows: the analysis hop length is determined as analysishoplength = numel(window)-overlaplength. the shiftpitch function assumes that there are 12 semitones in an octave, so the speedup factor used to stretch the audio is speedupfactor = 2^(-nsemitones/12). the speedup factor and analysis hop length determine the synthesis hop length for time-scale modification as synthesishoplength = round((1/speedupfactor)*analysishoplength).

the achievable pitch shift is determined by the window length (numel(window)) and overlaplength. to see the relationship, note that the equation for speedup factor can be rewritten as: nsemitones = -12*log2(speedupfactor), and the equation for synthesis hop length can be rewritten as speedupfactor = analysishoplengh/synthesishoplength. using simple substitution, nsemitones = -12*log2(analysishoplength/synthesishoplength). the practical range of a synthesis hop length is [1, numel(window)]. the range of achievable pitch shifts is:

  • max number of semitones lowered: -12*log2(numel(window)-overlaplength)

  • max number of semitones raised: -12*log2((numel(window)-overlaplength)/numel(window))

formant preservation

pitch shifting can alter the spectral envelope of the pitch-shifted signal. to diminish this effect, you can set preserveformants to true. if preserveformants is set to true, the algorithm attempts to estimate the spectral envelope using an iterative procedure in the cepstral domain, as described in [3] and [4]. for both the original spectrum, x, and the pitch-shifted spectrum, y, the algorithm estimates the spectral envelope as follows.

for the first iteration, envxa is set to x. then, the algorithm repeats these two steps in a loop:

  1. lowpass filters the cepstral representation of envxa to get a new estimate, envxb. the cepstralorder parameter controls the quefrency bandwidth.

  2. to update the current best fit, the algorithm takes the element-by-element maximum of the current spectral envelope estimate and the previous spectral envelope estimate:

    envxa=max(envxa,envxb).

the loop ends if either a maximum number of iterations (100) is reached, or if all bins of the estimated log envelope are within a given tolerance of the original log spectrum. the tolerance is set to log(10^(1/20)).

finally, the algorithm scales the spectrum of the pitch-shifted audio by the ratio of estimated envelopes, element-wise:

y=y×(envxbenvyb).

references

[1] driedger, johnathan, and meinard müller. "a review of time-scale modification of music signals." applied sciences. vol. 6, issue 2, 2016.

[2] driedger, johnathan. "time-scale modification algorithms for music audio signals." master's thesis. saarland university, saarbrücken, germany, 2011.

[3] axel roebel, and xavier rodet. "efficient spectral envelope estimation and its application to pitch shifting and envelope preservation." international conference on digital audio effects, pp. 30–35. madrid, spain, september 2005. hal-01161334

[4] s. imai, and y. abe. "spectral envelope extraction by improved cepstral method." electron. and commun. in japan. vol. 62-a, issue 4, 1997, pp. 10–17.

extended capabilities

c/c code generation
generate c and c code using matlab® coder™.

version history

introduced in r2019b

网站地图