how do you calculate frequency of squarewave

3 views (last 30 days)
Hey people!
Does anyone here know how to calculate the frequency of a squarewave inwhich the frequency increases over time?

Accepted Answer

Mathieu NOE
Mathieu NOE on 4 Nov 2022
hello
you can try this code
% dummy data
n = 10000;
x = 25*(0:n-1)/n;
y = sign(sin(x+0.25*x.^2));
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');
subplot(2,1,2),plot(t0_pos1(2:end),freq,'b.-','linewidth',2,'markersize',12);grid on
xlim([min(x) max(x)]);
legend('signal rate (frequency)');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!