I want to find instantaneous frequency over a window of a signal. I am taking a small part of the signal and trying to find the instantaneous frequency in that window. But that frequency is not matching with the actual frequency of the signal. Below is my code.
close all; clear all; clc
fs = 25000;
T = 0.5;
t = 0:1/fs:T;
n = length(t);
m = floor(n/4);
f1 = 100;
z1 = cos(2*pi*f1*t(1:m));
f1 = 200;
z2 = cos(2*pi*f1*t(1:m));
f1 = 300;
z3 = cos(2*pi*f1*t(1:m));
f1 = 400;
z4 = cos(2*pi*f1*t(1:(n-3*m)));
z = [z1 z2 z3 z4];
window = 100;
wStart = 1;
wEnd = wStart + window;
freqs = [];
while wEnd < length(z)
x = z(wStart:wEnd);
y = t(wStart:wEnd);
h=hilbert(x);
unrolled_phase = unwrap(angle(h));
dx = diff(unrolled_phase);
dy = diff(y);
p = dx./dy;
inst_freq = p/(2*pi) + 2*pi;
freqs = [freqs inst_freq];
wStart = wEnd;
wEnd = wStart + window;
end
The plot of 'freqs' is this:
I think there is something wrong with the way I am calculating the frequencies but I am not sure. Can anyone please help? I need to get the frequencies only as 100,200,300 and 400 as evident from the code. All I want to do is find the beginning point(time) of each frequency