Recreating Sine Wave from pitch detection

8 views (last 30 days)
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!

Accepted Answer

jibrahim on 29 Aug 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();
% 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();
% 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],...
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);

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 29 Aug 2022
Attached is a demo that creates a sound. Perhaps you can adapt it to change the frequency and amplitude envelope to whatever you want.


Find more on Audio Plugin Creation and Hosting in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by