-
Notifications
You must be signed in to change notification settings - Fork 3
/
detect_clipping.m
81 lines (61 loc) · 1.81 KB
/
detect_clipping.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
function [ C clip_cnt ] = detect_clipping( Y , FS , level_dB )
%function [ C clip_cnt ] = detect_clipping( Y , FS , level_dB )
% return a ( clip by 6 ) matrix that stores all clipping intervals
%
% Matrix format:
% C [ channel sample_begin sample_end time_begin time_end clip_sign ]
%
% Arguments:
% Y : Audio trak
% FS : samplig freq.
% level_dB : clipping level in dB
%
if level_dB > 0
error( 'error: the clipping level must be less or equal than 0.' )
end
level = 10^(level_dB/20) ;
clip = false ;
t = 1/FS ;
sz = 20000 ;
step = sz ;
C = zeros(sz,6) ;
clip_cnt = 0 ;
for ch=1:size(Y,2)
clip_flag = false ;
clip_begin = 0 ;
clip_sign = 1 ;
for i=1:size(Y,1)
if not( clip_flag ) && ( Y(i,ch) >= level || Y(i,ch) <= -level )
clip_flag = true ;
clip_cnt = clip_cnt + 1 ;
clip_begin = i ;
clip_sign = sign(Y(i,ch)) ;
elseif clip_flag && ( Y(i,ch) < level && Y(i,ch) > -level )
clip_flag = false ;
C(clip_cnt,1) = ch ;
C(clip_cnt,2) = clip_begin ;
C(clip_cnt,3) = i-1 ;
C(clip_cnt,4) = clip_begin*t ;
C(clip_cnt,5) = (i-1)*t ;
C(clip_cnt,6) = clip_sign ;
if clip_cnt >= sz
C = [C ; zeros(step,6)] ;
sz = size(C,1) ;
end
end
end
if clip_flag
C(clip_cnt,1) = ch ;
C(clip_cnt,2) = clip_begin ;
C(clip_cnt,3) = i-1 ;
C(clip_cnt,4) = clip_begin*t ;
C(clip_cnt,5) = (i-1)*t ;
C(clip_cnt,6) = clip_sign ;
if clip_cnt >= sz
C = [C ; zeros(step,6)] ;
sz = size(C,1) ;
end
end
end
C = C(1:clip_cnt,:) ;
end