function processed_signal=ECG_highpass(noisyECG_withTrend,window_of_interest,sample_rate,polynomial_degree,span)
%  The strategy is to fit a polynom from 6th order to cancel the DC + low
%  filters
num_of_samples_per_quote=sample_rate*window_of_interest; 
num_of_quotes=ceil(numel(noisyECG_withTrend)/num_of_samples_per_quote);
processed_signal=[];

for counter=0:num_of_quotes-1
    num_of_samples=min([num_of_samples_per_quote,numel(noisyECG_withTrend)-counter*num_of_samples_per_quote]);       
    try
    quote_data=noisyECG_withTrend(1+counter*num_of_samples_per_quote-10:counter*num_of_samples_per_quote+num_of_samples+10);
    [p,s,mu] = polyfit(((1-10):(num_of_samples+10)),quote_data,polynomial_degree);  %6
    f_y = polyval(p,(1:num_of_samples)',[],mu);
    fitted=(noisyECG_withTrend(1+counter*num_of_samples_per_quote:counter*num_of_samples_per_quote+num_of_samples) - f_y');
    processed_signal = [processed_signal smooth(fitted,span)'];        % Detrend data  %fitted   
    catch
    quote_data=noisyECG_withTrend(1+counter*num_of_samples_per_quote:counter*num_of_samples_per_quote+num_of_samples);
    [p,s,mu] = polyfit((1:num_of_samples),quote_data,polynomial_degree);  %6
    f_y = polyval(p,(1:num_of_samples)',[],mu);
    fitted=(noisyECG_withTrend(1+counter*num_of_samples_per_quote:counter*num_of_samples_per_quote+num_of_samples) - f_y');
    processed_signal = [processed_signal smooth(fitted)'];        % Detrend data  %fitted    
    end
%        if chunks_counter==1
% cla
% plot(noisyECG_withTrend(1:counter*num_of_samples_per_quote+num_of_samples))
% hold all
% plot(processed_signal)
% waitforbuttonpress;
end



