diff options
Diffstat (limited to 'SD-VBS/benchmarks/texture_synthesis/src/matlab/modskew.m')
-rwxr-xr-x | SD-VBS/benchmarks/texture_synthesis/src/matlab/modskew.m | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/SD-VBS/benchmarks/texture_synthesis/src/matlab/modskew.m b/SD-VBS/benchmarks/texture_synthesis/src/matlab/modskew.m new file mode 100755 index 0000000..3de0e3c --- /dev/null +++ b/SD-VBS/benchmarks/texture_synthesis/src/matlab/modskew.m | |||
@@ -0,0 +1,183 @@ | |||
1 | function [chm, snrk] = modskew(ch,sk,p); | ||
2 | |||
3 | % Adjust the sample skewness of a vector/matrix, using gradient projection, | ||
4 | % without affecting its sample mean and variance. | ||
5 | % | ||
6 | % This operation is not an orthogonal projection, but the projection angle is | ||
7 | % near pi/2 when sk is close to the original skewness, which is a realistic | ||
8 | % assumption when doing iterative projections in a pyramid, for example | ||
9 | % (small corrections to the channels' statistics). | ||
10 | % | ||
11 | % [xm, snrk] = modskew(x,sk,p); | ||
12 | % sk: new skweness | ||
13 | % p [OPTIONAL]: mixing proportion between sk0 and sk | ||
14 | % it imposes (1-p)*sk0 + p*sk, | ||
15 | % being sk0 the current skewness. | ||
16 | % DEFAULT: p = 1; | ||
17 | |||
18 | % | ||
19 | % JPM. 2/98, IODV, CSIC | ||
20 | % 4/00, CNS, NYU | ||
21 | |||
22 | Warn = 0; % Set to 1 if you want to see warning messages | ||
23 | if ~exist('p'), | ||
24 | p = 1; | ||
25 | end | ||
26 | |||
27 | N=prod(size(ch)); % number of samples | ||
28 | me=mean2(ch); | ||
29 | ch=ch-me; | ||
30 | |||
31 | for n=2:6, | ||
32 | m(n)=mean2(ch.^n); | ||
33 | end | ||
34 | |||
35 | sd=sqrt(m(2)); % standard deviation | ||
36 | s=m(3)/sd^3; % original skewness | ||
37 | snrk = snr(sk, sk-s); | ||
38 | sk = s*(1-p) + sk*p; | ||
39 | |||
40 | % Define the coefficients of the numerator (A*lam^3+B*lam^2+C*lam+D) | ||
41 | |||
42 | A=m(6)-3*sd*s*m(5)+3*sd^2*(s^2-1)*m(4)+sd^6*(2+3*s^2-s^4); | ||
43 | B=3*(m(5)-2*sd*s*m(4)+sd^5*s^3); | ||
44 | C=3*(m(4)-sd^4*(1+s^2)); | ||
45 | D=s*sd^3; | ||
46 | |||
47 | a(7)=A^2; | ||
48 | a(6)=2*A*B; | ||
49 | a(5)=B^2+2*A*C; | ||
50 | a(4)=2*(A*D+B*C); | ||
51 | a(3)=C^2+2*B*D; | ||
52 | a(2)=2*C*D; | ||
53 | a(1)=D^2; | ||
54 | |||
55 | % Define the coefficients of the denominator (A2+B2*lam^2) | ||
56 | |||
57 | A2=sd^2; | ||
58 | B2=m(4)-(1+s^2)*sd^4; | ||
59 | |||
60 | b=zeros(1,7); | ||
61 | b(7)=B2^3; | ||
62 | b(5)=3*A2*B2^2; | ||
63 | b(3)=3*A2^2*B2; | ||
64 | b(1)=A2^3; | ||
65 | |||
66 | |||
67 | if 0, % test | ||
68 | |||
69 | lam = -2:0.02:2; | ||
70 | S = (A*lam.^3+B*lam.^2+C*lam+D)./... | ||
71 | sqrt(b(7)*lam.^6 + b(5)*lam.^4 + b(3)*lam.^2 + b(1)); | ||
72 | % grd = ch.^2 - m(2) - sd * s * ch; | ||
73 | % for lam = -1:0.01:1, | ||
74 | % n = lam*100+101; | ||
75 | % chp = ch + lam*grd; | ||
76 | % S2(n) = mean2(chp.^3)/abs(mean2(chp.^2))^(1.5); | ||
77 | % end | ||
78 | lam = -2:0.02:2; | ||
79 | figure(1);plot(lam,S);grid;drawnow | ||
80 | % snr(S2, S-S2) | ||
81 | |||
82 | end % test | ||
83 | |||
84 | % Now I compute its derivative with respect to lambda | ||
85 | |||
86 | d(8) = B*b(7); | ||
87 | d(7) = 2*C*b(7) - A*b(5); | ||
88 | d(6) = 3*D*b(7); | ||
89 | d(5) = C*b(5) - 2*A*b(3); | ||
90 | d(4) = 2*D*b(5) - B*b(3); | ||
91 | d(3) = -3*A*b(1); | ||
92 | d(2) = D*b(3) - 2*B*b(1); | ||
93 | d(1) = -C*b(1); | ||
94 | |||
95 | d = d(8:-1:1); | ||
96 | mMlambda = roots(d); | ||
97 | |||
98 | tg = imag(mMlambda)./real(mMlambda); | ||
99 | mMlambda = real(mMlambda(find(abs(tg)<1e-6))); | ||
100 | lNeg = mMlambda(find(mMlambda<0)); | ||
101 | if length(lNeg)==0, | ||
102 | lNeg = -1/eps; | ||
103 | end | ||
104 | lPos = mMlambda(find(mMlambda>=0)); | ||
105 | if length(lPos)==0, | ||
106 | lPos = 1/eps; | ||
107 | end | ||
108 | lmi = max(lNeg); | ||
109 | lma = min(lPos); | ||
110 | |||
111 | lam = [lmi lma]; | ||
112 | mMnewSt = polyval([A B C D],lam)./(polyval(b(7:-1:1),lam)).^0.5; | ||
113 | skmin = min(mMnewSt); | ||
114 | skmax = max(mMnewSt); | ||
115 | |||
116 | |||
117 | % Given a desired skewness, solves for lambda | ||
118 | |||
119 | if sk<=skmin & Warn, | ||
120 | lam = lmi; | ||
121 | warning('Saturating (down) skewness!'); | ||
122 | skmin | ||
123 | elseif sk>=skmax & Warn, | ||
124 | lam = lma; | ||
125 | warning('Saturating (up) skewness!'); | ||
126 | skmax | ||
127 | else | ||
128 | |||
129 | |||
130 | % The equation is sum(c.*lam.^(0:6))=0 | ||
131 | |||
132 | c=a-b*sk^2; | ||
133 | |||
134 | c=c(7:-1:1); | ||
135 | |||
136 | r=roots(c); | ||
137 | |||
138 | % Chose the real solution with minimum absolute value with the rigth sign | ||
139 | lam=-Inf; | ||
140 | co=0; | ||
141 | for n=1:6, | ||
142 | tg = imag(r(n))/real(r(n)); | ||
143 | if (abs(tg)<1e-6)&(sign(real(r(n)))==sign(sk-s)), | ||
144 | co=co+1; | ||
145 | lam(co)=real(r(n)); | ||
146 | end | ||
147 | end | ||
148 | if min(abs(lam))==Inf & Warn, | ||
149 | display('Warning: Skew adjustment skipped!'); | ||
150 | lam=0; | ||
151 | end | ||
152 | |||
153 | p=[A B C D]; | ||
154 | |||
155 | if length(lam)>1, | ||
156 | foo=sign(polyval(p,lam)); | ||
157 | if any(foo==0), | ||
158 | lam = lam(find(foo==0)); | ||
159 | else | ||
160 | lam = lam(find(foo==sign(sk))); % rejects the symmetric solution | ||
161 | end | ||
162 | if length(lam)>0, | ||
163 | lam=lam(find(abs(lam)==min(abs(lam)))); % the smallest that fix the skew | ||
164 | lam=lam(1); | ||
165 | else | ||
166 | lam = 0; | ||
167 | end | ||
168 | end | ||
169 | end % if else | ||
170 | |||
171 | % Modify the channel | ||
172 | chm=ch+lam*(ch.^2-sd^2-sd*s*ch); % adjust the skewness | ||
173 | chm=chm*sqrt(m(2)/mean2(chm.^2)); % adjust the variance | ||
174 | chm=chm+me; % adjust the mean | ||
175 | % (These don't affect the skewness) | ||
176 | % Check the result | ||
177 | %mem=mean2(chm); | ||
178 | %sk2=mean2((chm-mem).^3)/mean2((chm-mem).^2).^(3/2); | ||
179 | %sk - sk2 | ||
180 | %SNR=snr(sk,sk-sk2) | ||
181 | |||
182 | |||
183 | |||