summaryrefslogtreecommitdiffstats
path: root/SD-VBS/benchmarks/texture_synthesis/src/matlab/modskew.m
diff options
context:
space:
mode:
Diffstat (limited to 'SD-VBS/benchmarks/texture_synthesis/src/matlab/modskew.m')
-rwxr-xr-xSD-VBS/benchmarks/texture_synthesis/src/matlab/modskew.m183
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 @@
1function [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
22Warn = 0; % Set to 1 if you want to see warning messages
23if ~exist('p'),
24 p = 1;
25end
26
27N=prod(size(ch)); % number of samples
28me=mean2(ch);
29ch=ch-me;
30
31for n=2:6,
32 m(n)=mean2(ch.^n);
33end
34
35sd=sqrt(m(2)); % standard deviation
36s=m(3)/sd^3; % original skewness
37snrk = snr(sk, sk-s);
38sk = s*(1-p) + sk*p;
39
40% Define the coefficients of the numerator (A*lam^3+B*lam^2+C*lam+D)
41
42A=m(6)-3*sd*s*m(5)+3*sd^2*(s^2-1)*m(4)+sd^6*(2+3*s^2-s^4);
43B=3*(m(5)-2*sd*s*m(4)+sd^5*s^3);
44C=3*(m(4)-sd^4*(1+s^2));
45D=s*sd^3;
46
47a(7)=A^2;
48a(6)=2*A*B;
49a(5)=B^2+2*A*C;
50a(4)=2*(A*D+B*C);
51a(3)=C^2+2*B*D;
52a(2)=2*C*D;
53a(1)=D^2;
54
55% Define the coefficients of the denominator (A2+B2*lam^2)
56
57A2=sd^2;
58B2=m(4)-(1+s^2)*sd^4;
59
60b=zeros(1,7);
61b(7)=B2^3;
62b(5)=3*A2*B2^2;
63b(3)=3*A2^2*B2;
64b(1)=A2^3;
65
66
67if 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;
79figure(1);plot(lam,S);grid;drawnow
80% snr(S2, S-S2)
81
82end % test
83
84% Now I compute its derivative with respect to lambda
85
86d(8) = B*b(7);
87d(7) = 2*C*b(7) - A*b(5);
88d(6) = 3*D*b(7);
89d(5) = C*b(5) - 2*A*b(3);
90d(4) = 2*D*b(5) - B*b(3);
91d(3) = -3*A*b(1);
92d(2) = D*b(3) - 2*B*b(1);
93d(1) = -C*b(1);
94
95d = d(8:-1:1);
96mMlambda = roots(d);
97
98tg = imag(mMlambda)./real(mMlambda);
99mMlambda = real(mMlambda(find(abs(tg)<1e-6)));
100lNeg = mMlambda(find(mMlambda<0));
101if length(lNeg)==0,
102 lNeg = -1/eps;
103end
104lPos = mMlambda(find(mMlambda>=0));
105if length(lPos)==0,
106 lPos = 1/eps;
107end
108lmi = max(lNeg);
109lma = min(lPos);
110
111lam = [lmi lma];
112mMnewSt = polyval([A B C D],lam)./(polyval(b(7:-1:1),lam)).^0.5;
113skmin = min(mMnewSt);
114skmax = max(mMnewSt);
115
116
117% Given a desired skewness, solves for lambda
118
119if sk<=skmin & Warn,
120 lam = lmi;
121 warning('Saturating (down) skewness!');
122 skmin
123elseif sk>=skmax & Warn,
124 lam = lma;
125 warning('Saturating (up) skewness!');
126 skmax
127else
128
129
130% The equation is sum(c.*lam.^(0:6))=0
131
132c=a-b*sk^2;
133
134c=c(7:-1:1);
135
136r=roots(c);
137
138% Chose the real solution with minimum absolute value with the rigth sign
139lam=-Inf;
140co=0;
141for 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
147end
148if min(abs(lam))==Inf & Warn,
149 display('Warning: Skew adjustment skipped!');
150 lam=0;
151end
152
153p=[A B C D];
154
155if 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
168end
169end % if else
170
171% Modify the channel
172chm=ch+lam*(ch.^2-sd^2-sd*s*ch); % adjust the skewness
173chm=chm*sqrt(m(2)/mean2(chm.^2)); % adjust the variance
174chm=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