Find period cycle and frequency for signal

8 views (last 30 days)
Shanmuka
Shanmuka on 10 Jan 2023
Commented: Star Strider on 10 Jan 2023
Hi,
And For 50Hz frequency, need to plot 1000 cycles, with varrying frequency of 49.97/49.98/50.01.50.02 adn show the varrying load on graph i.e.
(Period cycle and measured frequency)
How do i need to find period and frequency of signal?
clc;
clear all;
close all;
% CONSTANTS
fs=2400;
fo=50;
t=0:1000;
t2=0.5;
% Geneeration of sine wave
xp=sin(2*pi*(fo/fs)*t);
% upward zero crossing detector
UpZC = @(a) find(a(1:end-1) <= 0 & a(2:end) > 0);
% downward zero crossing detector
DownZC = @(a) find(a(1:end-1) >= 0 & a(2:end) < 0);
% interploator formula
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1);
ZXi = sort([UpZC(xp),DownZC(xp)]);
% zerocrossing detector with interpolation
ZX = ZeroX(t(ZXi),xp(ZXi),t(ZXi+1),xp(ZXi+1));
if xp(end)==0
ZX(end+1) = t(end);
end
xx= zeros(1,length(ZX));
xx1= zeros(1,length(ZX));
for i=1:length(ZX)
if rem(i,2)==1
xx(1,i)=ZX(i)+t2;
xx1(1,i)=ZX(i)-t2;
else
xx(1,i)=ZX(i)-t2;
xx1(1,i)=ZX(i)+t2;
end
end
y1= sin(2*pi*(fo/fs)*t2);
figure(1)
plot(t,xp, '-b')
hold on
plot(ZX,zeros(1,length(ZX)),'go')
hold on;
plot(xx,zeros(1,length(ZX))+y1,'r*')
hold on
plot(xx1(2:end),zeros(1,length(ZX)-1)-y1,'r*')
grid on;
legend('Sine wave Signal', 'zero crossing point','Interpolated Zero-Crossing')
xlabel('Time');
ylabel('Amplitude');

Answers (1)

Star Strider
Star Strider on 10 Jan 2023
Finding the approximate zero-crossings is straightforward, and the subsequent interpolation involves a loop to get the exact zero-crossings —
% CONSTANTS
fs=2400;
fo=50;
t=0:1000;
t2=0.5;
N = numel(t);
% Geneeration of sine wave
xp=sin(2*pi*(fo/fs)*t);
zxi = find(diff(sign(xp))); % Approximate Indices Of Zero-Crossings
for k = 1:numel(zxi)
idxrng = max(1,zxi(k)-1) : min(N,zxi(k)+1); % Index Range For Interpolation
txc(k) = interp1(xp(idxrng), t(idxrng), 0); % Interpolate
end
figure
plot(t, xp, 'DisplayName','Data')
hold on
plot(txc, zeros(size(txc)), 'rs', 'DisplayName','Zero-Crossings')
hold off
grid
xlabel('t')
ylabel('xp')
for k = 1:numel(zxi)
idxrng = max(1,zxi(k)-2) : min(N,zxi(k)+2); % Index Range For Interpolation
DM = [t(idxrng); ones(size(idxrng))].'; % Design Matrix
B = DM \ xp(idxrng).'; % Parameter Vector
txv(k) = -B(2) / B(1); % Interpolate
end
figure
plot(t, xp, 'DisplayName','Data')
hold on
plot(txv, zeros(size(txv)), 'rs', 'DisplayName','Zero-Crossings')
hold off
grid
xlabel('t')
ylabel('xp')
The first loop uses the interp1 function and the second uses simple linear regression.
.
  2 Comments
Shanmuka
Shanmuka on 10 Jan 2023
But, I want to plot rate of change of frequency for 3 frequencies which are added to single sine signal and find out difference in plot with respect to different frequencies
Star Strider
Star Strider on 10 Jan 2023
It is not easy to follow your code.
If you have more periodic (or aperiodic) curves to analyse, run my code for each one of them, or if they all have the same time vector, the sum or product of them as well, if that is what you are doing. You can combine them easily if they have different frequecies, however not if they have different time vectors.
If they have different time vectors, you will have to analyse each one individually. That should be straightforward by just repeating my code with each of them.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!