
How can I plot the spectrum of two segments from this signal?
3 views (last 30 days)
Show older comments
Hannah Oduntan
on 30 Dec 2018
Commented: Star Strider
on 30 Dec 2018
In the signal attached, how can I get the spectrum for the first segment of the signal (the signal before the major peak) and and the third segment of the signal (the signal after the major peak)? This is the code I have used, but unfortunately the figure comes up blank for some reason.
0 Comments
Accepted Answer
Star Strider
on 30 Dec 2018
Try this:
D = load('signals (1).mat');
Signal = D.Signal; % Get ‘Signal’
N = length(Signal);
t = linspace(0, 1, N) * N; % Create Time Vector
[pk,loc] = findpeaks(Signal, 'MinPeakHeight',1); % Determine Indices Of Various Components
[trofs,trlocs] = findpeaks(-Signal);
ltidx = find(trlocs < loc, 1, 'last');
gtidx = find(trlocs > loc, 1, 'first');
Out{1} = {Signal(1:trlocs(ltidx)); Signal(trlocs(ltidx)+1:trlocs(gtidx)-1); Signal(trlocs(gtidx):end)}; % ‘Signal’ Cell Array
Out{2} = {t(1:trlocs(ltidx)); t(trlocs(ltidx)+1:trlocs(gtidx)-1); t(trlocs(gtidx):N)}; % ‘t’ Cell Array
figure
plot(Out{2}{1}, Out{1}{1}, 'r')
hold on
plot(Out{2}{2}, Out{1}{2}, 'g')
plot(Out{2}{3}, Out{1}{3}, 'b')
hold off
grid
xlim([min(t) max(t)])
figure
subplot(2, 2, 1);
plot(Out{2}{1}, Out{1}{1}, 'r')
grid
subplot(2, 2, 2);
plot(Out{2}{2}, Out{1}{2}, 'g')
grid
subplot(2, 2, 3);
plot(Out{2}{3}, Out{1}{3}, 'b')
grid
FFTN = 2^14;
% Fv = linspace(0, 1, fix(FFTN/2)+1);
for k1 = 1:numel(Out{1})
Ts = mean(diff(Out{2}{k1}));
Fs = 1/Ts;
Fn = Fs/2;
FTSeg{k1} = fft(Out{1}{k1},FFTN);
Fv{k1} = linspace(0, 1, fix(numel(FTSeg{k1})/2)+1)*Fn;
Iv{k1} = 1:numel(Fv{k1});
end
ttlcs = {'Signal Before Peak', 'Peak', 'Signal After Peak'};
figure
for k1 = 1:numel(Out{1})
subplot(numel(Out{1}), 1, k1)
plot(Fv{k1}, abs(FTSeg{k1}(Iv{k1})*2))
xlim([0 0.02])
title(ttlcs(k1))
end
The Plot —

2 Comments
More Answers (0)
See Also
Categories
Find more on Spectral Measurements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!