function out = clip_detect(data,headroom_dBFS,num_clips)
%---detects clipping signals above a certain headroom---
clips = 0; %number of clips
over = 0; %goes to 1 if you clip given the criteria
headroom = 10.^(headroom_dBFS/20); %dBFS to decimal conversion
max_val = 1/headroom; %get value for typical float representation where 1 means 0 dBFS
%data=reshape(data,[],1); %reshape matrix to vector
temp = size(data); 
for i=(1:temp)
    if(abs(data(i)) >= max_val) %if the signal goes over the headroom
        clips=clips+1;
        if (clips >= num_clips) %if the signal clips too many times in a row
            over = 1;
            break; %exit and return 1 when the signal is clipped
        end
    else
        clips=0; %reset counter
    end
end
out = over;

%Measuring directional characteristics of sound sources
%Made by Benjamin Nimmerrichter 2022/23 for BUT