function [winpeaksrmstime, winpeaksrmsnumt, winpeaksrmsfract, minpeakheight] = winpeaksrms(npt, overlap, lwinsvec, midwinpoints, time, B_output, heightfact, Brmsquiet)

% minpeakheight = mult * Brmsquiet;
% rmsthreshold = mult * Brmsquiet;
% [~, locs] = findpeaks(abs(B_output(1:npt)),'MinPeakHeight',rmsthreshold);
% [~, locst] = findpeaks(abs(B_output(1:npt)),'MinPeakHeight',Brmsquiet);
% winpeaksrmstime{1} = {locs' time(locs)'};
% winpeaksrmsnumt(1) = length(locs);
% winpeaksrmsfract(1) = length(locs)/length(locst);

for i = 1:length(midwinpoints)
% winstart = (i-1)*(npt-overlap); % start of vector overlaps with previous window
% winend = winstart + npt - 1; % end of vector is npt points later

[pkst, locst] = findpeaks(abs(B_output(lwinsvec(i):lwinsvec(i+1))),'MinPeakHeight',Brmsquiet);
if heightfact > 0
    minpeakheight(i) = quantile(pkst,heightfact);
    [~, locs] = findpeaks(abs(B_output(lwinsvec(i):lwinsvec(i+1))),'MinPeakHeight',minpeakheight(i));
else
    minpeakheight(i) = Brmsquiet;
    locs = locst;
end

locs = locs+lwinsvec(i)-1;
[winpeaksrmstimev, I] = sort(time(locs));
winpeaksrmstime{i} = {locs(I)' winpeaksrmstimev'};
winpeaksrmsnumt(i) = length(locs);
winpeaksrmsfract(i) = length(locs)/length(locst);
end
% if length(winpeaksrmsnumt) < length(midwinpoints)
% [~, locs] = findpeaks(abs(B_output(end-npt:end)),'MinPeakHeight',rmsthreshold);
% [~, locst] = findpeaks(abs(B_output(end-npt:end)),'MinPeakHeight',Brmsquiet);
% locs = locs+length(time)-npt-1;
% winpeaksrmstime{floor(length(B_output)/(npt-overlap))} = {locs' time(locs)'};
% winpeaksrmsnumt(floor(length(B_output)/(npt-overlap))) = length(locs);
% winpeaksrmsfract(floor(length(B_output)/(npt-overlap))) = length(locs)/length(locst);
% end
end