Is it possible to calculate variating signal frequency?

1 view (last 30 days)
I am attempting to analyze the stability of a signal by calculating the frequency to see if it is the same over the entire time period. I would need to know if it changes and by how much. My plot looks like this:
It has a lot of data points (about 20000 lines). Is this possible to do in MATLAB?
I have attached the original text file if that is helpful.

Accepted Answer

Star Strider
Star Strider on 17 Mar 2023
Theere are likely several ways to analyse this.
One option is to use findpeaks to get the distances between peaks. Another is to use pspectrum to calculate the spectrogram to see if the frequencies vary.
It turns out that they do —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1328065/time0.txt')
T1 = 21000×2 table
Var1 Var2 _______________________ ________ 03/17/2023 09:01:32.839 0.010975 03/17/2023 09:01:32.839 0.009688 03/17/2023 09:01:32.839 0.009688 03/17/2023 09:01:32.839 0.011619 03/17/2023 09:01:32.839 0.009366 03/17/2023 09:01:32.839 0.011619 03/17/2023 09:01:32.840 0.010653 03/17/2023 09:01:32.840 0.010653 03/17/2023 09:01:32.840 0.012584 03/17/2023 09:01:32.840 0.013872 03/17/2023 09:01:32.840 0.014515 03/17/2023 09:01:32.840 0.011941 03/17/2023 09:01:32.840 0.012584 03/17/2023 09:01:32.840 0.017411 03/17/2023 09:01:32.840 0.012584 03/17/2023 09:01:32.840 0.016446
t = T1{:,1};
y = T1{:,2};
[pks,locs] = findpeaks(y, 'MinPeakProminence',2);
dlocs = diff([locs(1); locs]);
figure
yyaxis left
plot(t, y)
hold on
plot(t(locs), pks, '|m')
hold off
ylabel('Signal)')
yyaxis right
plot(t(locs), dlocs)
ylabel('Peak Distance Differences')
grid
xlabel('Time')
figure
pspectrum(y,t,'spectrogram')
colormap(turbo)
% L = size(T1,1);
% Thrshld = median(y);
% tidx = find(diff(sign(y-Thrshld)));
% for k = 1:numel(tidx)-1
% idxrng = max(1,tidx(k)-1) : min(L,tidx(k)+1)
% Y = y(idxrng)
% T = t(idxrng)
% tv(k,:) = interp1(y(idxrng), t(idxrng), Thrshld);
% end
%
% ddx = diff([tv(1); tv]);
%
% figure
% yyaxis left
% plot(t, y)
% % hold on
% % plot(t(locs), pks, '|m')
% % hold off
% ylabel('Signal')
% yyaxis right
% plot(tv, ddx)
% ylabel('Threshold Crossing Differences')
% grid
Attempting to determine the ‘exact’ instnaces at which tie signal crosses a specific threshold (chosen here as the median value of ‘y’) fails because the times in ‘t’ are not unique. This approach could work on a different data set, so I commented it out rather than removing it cokmpletely.
.

More Answers (1)

Mathieu NOE
Mathieu NOE on 17 Mar 2023
hello Niki
try this below
frequency is computed by finding a "zero" (or any other threshold value) crossing point with your signal and simply computing the time delta
hope it helps
% load data
T = readtable('time0.txt');
x = convertTo(T.Var1,'excel');
x = x - x(1); % time starts at 0 now
y = T.Var2;
threshold = 0.5*max(y); % 50% of peak amplitude
t0_pos1 = find_zc(x,y,threshold);
period = diff(t0_pos1); % delta time of crossing points
freq = 1./period; % signal frequency
figure(1)
subplot(2,1,1),plot(x,y,'b-',t0_pos1,threshold*ones(size(t0_pos1)),'.r','linewidth',2,'markersize',12);grid on
legend('signal','signal positive slope crossing points');
xlabel('Time (s)');
xlim([0 max(x)]);
subplot(2,1,2),plot(t0_pos1(2:end),freq,'b.-','linewidth',2,'markersize',12);grid on
xlim([min(x) max(x)]);
xlim([0 max(x)]);
legend('signal rate (frequency)');
xlabel('Time (s)');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Zx] = find_zc(x,y,threshold)
% positive slope "zero" crossing detection, using linear interpolation
y = y - threshold;
zci = @(data) find(diff(sign(data))>0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
Zx = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
end

Community Treasure Hunt

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

Start Hunting!