How can I call a script in another script using the built in function 'function'?
Afficher commentaires plus anciens
How can I call the FindPeaks script that I've written into the Spectrum script? In the Spectrum script I have added the built in function 'findscripts' but I want to take it out and use my own function FindPeaks function, how can I do this? You can find the signal file attached.
%% FindPeaks
Signal;
y = Signal;
x = 0:2500;
m = islocalmax(y);
plot(Signal);
hold on;
plot(x(m), y(m),'k*')
%% Spectrum
Signal;
L = length(Signal);
T = linspace(0, 1, L) * L;
[peak,location] = findpeaks(Signal, 'MinPeakHeight',1);
[a,b] = findpeaks(-Signal);
peakend = find(b < location, 1, 'last');
peakstart = find(b > location, 1, 'first');
Output{1} = {Signal(1:b(peakend)); Signal(b(peakend)+1:b(peakstart)-1); Signal(b(peakstart):end)}; % ‘Signal’ Cell Array
Output{2} = {T(1:b(peakend)); T(b(peakend)+1:b(peakstart)-1); T(b(peakstart):L)}; % ‘T’ Cell Array
figure
plot(Output{2}{1}, Output{1}{1}, 'r'); % Assigns red colour to signal before eye blink peak
hold on % Keeps plots in the current axes so new plots added don't delete existing plots
plot(Output{2}{2}, Output{1}{2}, 'g'); % Assigns green colour to signal at eye blink peak
plot(Output{2}{3}, Output{1}{3}, 'b');
hold off
grid
xlim([min(T) max(T)])
figure
subplot(2, 2, 1);
plot(Output{2}{1}, Output{1}{1}, 'r')
grid
subplot(2, 2, 2);
plot(Output{2}{2}, Output{1}{2}, 'g')
grid
subplot(2, 2, 3);
plot(Output{2}{3}, Output{1}{3}, 'b')
grid
number = 2^14;
for k = 1:numel(Output{1})
Ts = mean(diff(Output{2}{k}));
Fs = 1/Ts;
Fn = Fs/2;
seg{k} = fft(Output{1}{k},number);
Fvec{k} = linspace(0, 1, fix(numel(seg{k})/2)+1)*Fn; % Linspace function to generate linearly spread vectors
Ivec{k} = 1:numel(Fvec{k});
end
titlename = {'Spectrum of Signal Before Peak', 'Spectrum of Eye Blink Peak', 'Spectrum of Signal After Peak'};
figure
for k = 1:numel(Output{1})
subplot(numel(Output{1}), 1, k)
plot(Fvec{k}, abs(seg{k}(Ivec{k})*2))
xlim([0 0.02])
title(titlename(k))
end
Réponses (1)
Walter Roberson
le 11 Jan 2019
0 votes
near the beginning
findpeaks = @FindPeaks;
Catégories
En savoir plus sur Descriptive Statistics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!