You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: avg_beat_calculator_phase_domain.m
    path of file: ./repos/OSET/matlab/tools/ecg
    
    the code of the file until where you have to start completion: function [ECGmean, ECGsd, meanPhase, ECGmedian, SamplesPerBin] = avg_beat_calculator_phase_domain(x,phase,bins,flag)
%
% [ECGmean,ECGsd,meanPhase] = avg_beat_calculator_phase_domain(x,phase,bins,flag)
% Calculation of the mean and SD of ECG waveforms in different beats
%
% inputs:
%   x: input ECG signal
%   phase: ECG phase

    
function [ECGmean, ECGsd, meanPhase, ECGmedian, SamplesPerBin] = avg_beat_calculator_phase_domain(x,phase,bins,flag)
%
% [ECGmean,ECGsd,meanPhase] = avg_beat_calculator_phase_domain(x,phase,bins,flag)
% Calculation of the mean and SD of ECG waveforms in different beats
%
% inputs:
%   x: input ECG signal
%   phase: ECG phase
%   bins: number of desired phase bins
%   flag
%     1: aligns the baseline on zero, by using the mean of the first 10%
%     segment of the calculated mean ECG beat
%     0: no baseline alignment
%
% outputs:
%   ECGmean: mean ECG beat (in phase domain)
%   ECGsd: standard deviation of ECG beats (unbiased estimate)
%   meanPhase: the corresponding phase for one ECG beat
%   ECGmedian: median ECG beat (in phase domain)
%   SamplesPerBin: number of samples averaged in each phase bin (used for statistical analysis)
%
%   Revision History:
%       2008: First release
%       2021: Median beat calculation and SamplesPerBin added
%       2023: Renamed from deprecated version MeanECGExtraction
% 
%   Reza Sameni, 2006-2023
%   The Open-Source Electrophysiological Toolbox
%   https://github.com/alphanumericslab/OSET

meanPhase = zeros(1, bins);
ECGmean = zeros(1, bins);
ECGmedian = zeros(1, bins);
ECGsd = zeros(1, bins);
SamplesPerBin = zeros(1, bins);

I = find( phase >= (pi-pi/bins)  | phase < (-pi+pi/bins) );
SamplesPerBin(1) = length(I);
if ~isempty(I)
    meanPhase(1) = -pi;
    ECGmean(1) = mean(x(I));
    ECGmedian(1) = median(x(I));
    ECGsd(1) = std(x(I));
else
    meanPhase(1) = 0;
    ECGmean(1) = 0;
    ECGmedian(1) = 0;
    ECGsd(1) = -1;
end