# how to isolate harmonic component from figure

1 view (last 30 days)
Hassan Abdelazeem on 15 Mar 2022
Commented: Simon Chan on 16 Mar 2022
Dear all ,
If I have a signal with harmonic how can I automatical know the index (the position of each number of the harmonic) . In another way , I need to know when the harmonic happens.
The data are in the attachment file

Simon Chan on 15 Mar 2022
Try the following to see whether this is what you want or not.
clear; clc;
x = 1:length(xx);
% Separate entire signal into 3 sections
upperTh = 0.95*max(xx);
lowerTh = 0.95*min(xx);
index = xx<upperTh & xx>lowerTh;
istart = find(diff(index)==1);
iend = find(diff(index)==-1);
profile = arrayfun(@(r,s) xx(r:s),istart,iend,'uni',0); % 3 separate profiles
% Determine the positions of the turning points
[~,idx1] = cellfun(@(x) min(diff(diff(x)>0)),profile);
[~,idx2] = cellfun(@(x) max(diff(diff(x)>0)),profile);
rangeIdx = sort([idx1, idx2],2) + istart;
% Adding some offset besides turning points to find min/max
offset = 5;
harmonic = arrayfun(@(r,s) xx(r-offset:s+offset),rangeIdx(:,1),rangeIdx(:,2),'uni',0);
[maxValue,maxIdx] = cellfun(@max,harmonic);
[minValue,minIdx] = cellfun(@min,harmonic);
% Plot result only (Can be ignored)
for k = 1:3
subplot(3,1,k)
plot(x(rangeIdx(k,1)-offset:rangeIdx(k,2)+offset),harmonic{k});
title({sprintf('MinValue = %.4f, MinPos = %d',minValue(k),rangeIdx(k,1)-offset+minIdx(k)-1);...
sprintf('MaxValue = %.4f, MaxPos = %d',maxValue(k),rangeIdx(k,1)-offset+maxIdx(k)-1)});
grid on
end ##### 2 CommentsShowHide 1 older comment
Simon Chan on 16 Mar 2022
Use another approach to retrieve the positions of the hystersis loops as follows.
After that, you can use the start and end position index for further actions.
clear; clc;
xx = m3rdr(:,1);
yy = m3rdr(:,2);
% Extract the holes from the polygons
pgon = polyshape(xx,yy); % Build the polyshape, ignore the warning
Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce inaccurate or unexpected results. Input data has been modified to create a well-defined polyshape.
polyout = holes(pgon); % Extract holes which are the hystersis loops
figure
subplot(2,2,1)
plot(pgon); % Display original signals
ax1 = gca;
title('Original shape');
xLimit = ax1.XLim;
yLimit = ax1.YLim;
grid(ax1,'on');
subplot(2,2,2)
plot(polyout); % Display the extracted holes
ax2 = gca;
title('Extract holes coordinates');
ax2.XLim = xLimit;
ax2.YLim = yLimit;
grid(ax2,'on');
for k = 1:length(polyout)
subplot(2,2,k+2)
% Start position of the extarcted hole
xstart = polyout(k).Vertices(1,1);
% End position of the extarcted hole, the last vertice is not used
xend = polyout(k).Vertices(end-1,1);
startIdx = find(xx == xstart);
endIdx= find(xx == xend);
plot(xx(startIdx:endIdx+1),yy(startIdx:endIdx+1));
hold on
grid on
plot(xx(startIdx),yy(startIdx),'g*'); % Start point showing green dot
plot(xx(endIdx+1),yy(endIdx+1),'r*'); % Last point showing red dot
title({sprintf('Start Position = %d',startIdx);...
sprintf('End Position = %d',endIdx+1)});
hold off
end 