Recreating Sine Wave from pitch detection

Dear Community, I am looking for a way to rebuild a signal from the pitch information from an other signal. Best scenario would be to read in a sine wave with changing pitch, extract the pitch information and then to be able to apply it to a sine wave. Any help is very much appreciated! Thanks!

 Respuesta aceptada

jibrahim
jibrahim el 29 de Ag. de 2022
Hi Hans,
Here is some code that approximates what you want. It uses:
  • audioOscillator to create a frequency-varying sine wave signal
  • pitch to estimate the pitch of a signal
  • timescope to visualize the original and reconstructed signals.
% Create the original frequency-varying sine wave signal
src = audioOscillator;
F = [100:500 499:-1:100];
% We will change the frequency every 10 frames
numFramesPerFreq = 10;
frameLength = src.SamplesPerFrame;
sig = zeros(numFramesPerFreq*size(F,2)*frameLength,1);
% Build the sine wave
for index=1:size(F,2)
src.Frequency = F(index);
for index2=1:numFramesPerFreq
startInd = (index-1)*numFramesPerFreq*frameLength+(index2-1)*frameLength + 1;
sig(startInd:startInd+frameLength-1) = src();
end
end
% Estimate the pitch of the signal
% A pitch estimate is generated for every "hop" of samples
p = pitch(sig,src.SampleRate,WindowLength=1024,OverlapLength=896);
% Hop length is window length - overlap length
hopLength = 1024-896;
% This is the reconstructed signal (based on pitch estimates)
sig2 = zeros(hopLength*size(p,1),1);
% We will build this signal
src = audioOscillator;
frameLength = hopLength;
src.SamplesPerFrame = hopLength;
for index=1:size(p,1)
% For every frame, use estimated pitch
osc.Frequency = p(index);
sig2((index-1)*frameLength+1:index*frameLength) = src();
end
% Make original and reconstructed signals the same length
L = min(size(sig,1),size(sig2,1));
sig = sig(1:L);
sig2 = sig2(1:L);
% Use a scope to visualize the signals
scope = timescope(SampleRate=src.SampleRate,...
LayoutDimensions=[2 1],...
NumInputPorts=2,...
TimeSpan=.1,...
YLimits=[-1 1]);
scope.ActiveDisplay = 2;
scope.YLimits = [-1 1];
numFrames = size(sig,1)/hopLength;
for index=1:numFrames
in1 = sig((index-1)*hopLength+1:index*hopLength);
in2 = sig((index-1)*hopLength+1:index*hopLength);
scope(in1,in2)
end

6 comentarios

Hans Buchele
Hans Buchele el 1 de Sept. de 2022
Hey Jibrahim,
Thanks for your suggestions but I am not getting consistent results. If I create an sinus wave of 220 hertz, the pitch from [f0,loc] = pitch(x,fs) is displayed correctly, but with higher frequencies, for example 1000 hertz, the results are around 330 hertz. It must be user error but I can not figure out the problem.
jibrahim
jibrahim el 1 de Sept. de 2022
Can you include your code? That would help.
Hans Buchele
Hans Buchele el 1 de Sept. de 2022
Sorry I wasn't on my computer before.
clc;
clearvars;
A = 1;
fs = 44100;
dt = 1/fs;
t = 0:dt:10;
f1 = 320;
x = A*sin(2*pi*f1*t);
y = A*sin(2*pi*f1*t);
filename = sprintf('%d.wav',f1);
stereo_mtx = [x(:), y(:)];
audiowrite(filename, stereo_mtx, fs, 'BitsPerSample', 24);
x = audioread(filename);
[f0,loc] = pitch(x,fs);
Hi Hans,
I can reproduce this. It seems you are hitting a limitation of the pitch function. Its algorithm works well for speech signals, but misfires for such test signals. I will gather more info on this soon.
In the meantime, you can use the AI-powered pitchnn function instead.It behaves much better with such signals.
You first download some required files:
downloadFolder = fullfile(tempdir,'crepeDownload');
loc = websave(downloadFolder,'https://ssd.mathworks.com/supportfiles/audio/crepe.zip');
crepeLocation = tempdir;
unzip(loc,crepeLocation)
addpath(fullfile(crepeLocation,'crepe'))
Then you should be able to use pitchnn:
clc;
clearvars;
A = 1;
fs = 44100;
dt = 1/fs;
t = 0:dt:10;
f1 = 1000;
x = A*sin(2*pi*f1*t);
y = A*sin(2*pi*f1*t);
filename = sprintf('%d.wav',f1);
stereo_mtx = [x(:), y(:)];
audiowrite(filename, stereo_mtx, fs, 'BitsPerSample', 24);
x = audioread(filename);
pitchnn(x,fs); % No poutput specified gives you a plot
[f0,loc] = pitchnn(x,fs);
A little more info. For pure tones, you should use instfreq. All of the pitch algorithms are exploiting the harmonic structure of voice--they're really not designed for pure tones, and simpler algorithms like instfreq will perform best:
A = 1;
fs = 44100;
dt = 1/fs;
t = 0:dt:10;
f1 = 1000;
x = A*sin(2*pi*f1*t);
y = A*sin(2*pi*f1*t);
filename = sprintf('%d.wav',f1);
stereo_mtx = [x(:), y(:)];
instfreq(stereo_mtx,fs);
More info on the pitch function: It has a range parameter which is set by default to handle speech signal (default range is [50, 400] Hz). You can adjust the range for your case:
A = 1;
fs = 44100;
dt = 1/fs;
t = 0:dt:10;
f1 = 1000;
x = A*sin(2*pi*f1*t);
y = A*sin(2*pi*f1*t);
filename = sprintf('%d.wav',f1);
stereo_mtx = [x(:), y(:)];
pitch(stereo_mtx,fs,Range=[50 5000]);
The function pitchnn should work for both types of sound (speech and sine waves are fine)
Hans Buchele
Hans Buchele el 2 de Sept. de 2022
Great thank you!

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 29 de Ag. de 2022

0 votos

Attached is a demo that creates a sound. Perhaps you can adapt it to change the frequency and amplitude envelope to whatever you want.

Categorías

Productos

Versión

R2022a

Etiquetas

Preguntada:

el 28 de Ag. de 2022

Comentada:

el 2 de Sept. de 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by