fs = 48000;
N = 1024;
f = 93.75;
N2 = N;
n = 0:(N*2);
x = sin(2*pi*f/fs*n)
% FFT taken hop samples apart
hop = 2;
X1 = fft(x(1:N));
X2 = fft(x(N+hop:N+hop+N-1);
% Take the end and last hop (2) samples of FFT
X1_bin = X(end-hop);
X2_bin = X(end);
% Compute the phase difference using complex numbers
phase_diff_complex = angle(X2_bin / (X1_bin));
%phase_diff_complex = unwrap(angle(X2_bin)) - unwrap(angle(X1_bin));
% Calculate the expected phase shift due to a hop of t samples for f0
expected_phase_shift = -2 * pi * f0 * hop / fs;
% Compute the phase difference between actual and expected
phase_difference = phase_diff_complex - expected_phase_shift;
% Normalize the phase difference to be within the range [-pi, pi]
phase_difference = mod(phase_difference + pi, 2*pi) - pi;
% Compute the frequency offset
frequency_offset = (phase_difference * fs) / (2 * pi * hop);
%frequency_offset = frequency_offset + 24000
Code language: Matlab (matlab)