
Добавил:
debilX2
t.me
Фулл всегда есть, ФОЭ ТОЭ ТЭЦ Электроника, КЭТ ИиКГ и тд https://t.me/whitedevil752
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:lab2 вроде / untitled
.m % main GaussFilter_2D
close all
clear all
clc
STEP = 1 % 2-D Filter Mask
sigma = 20
ssq = sigma^2;
r = 1/(2*ssq);
GaussianDieOff = .0001;
% Design the filters - a gaussian
pw = 1:30; % possible widths
width = find(exp(-(pw.*pw)*r)>GaussianDieOff,1,'last')
if isempty(width), width = 1;end
% the user entered a really small sigma
L = width % Size of the window [Samples]
L0 = floor(L/2)
w = zeros(L);
for i = 1:L
for j = 1:L
w(i,j) = exp(-((j-L0)^2+(i-L0)^2)/(4*ssq));
end;
end;
figure(100), imshow(w), title('Gaussian')
figure(101), mesh(w), title('Gaussian')
STEP = 2
w1 = zeros(L,1);
for i = 1:L
for j = 1:L
w1(i,j) = w(i,j)*(-1)^(i+j);
end;
end;
W = abs(fft2(w1)); size(W) % Fourier Transform
STEP = 3
P = W.^2; % Power Spectrum of Gauss Window
X3 = L0;
PMAX = max(P(X3,:));
figure(302), plot(1:L,P(X3,1:L)/PMAX), grid
title('Power Transmission Coeff P')
ylabel('P(f)')
STEP = 4 % Inverse Transform
P1 = zeros(L);
for i = 1:L
for j = 1:L
P1(i,j) = P(i,j)*(-1)^(i+j);
end;
end;
C = abs(ifft2(P1)); % Inverse Fourier on Power Spectrum
X4 = L0;
CMAX = max(C(X4,:));
figure(402), plot(1:L,C(X4,1:L)/CMAX),
title('Normalized Correlation C'), grid
ts = input('STOP4','s');
STEP = 5 %NormalNoise
m = 256; n = 256;
K = 1 % number of fields
% NORMAL FIELD
mu = 30; stdev = 30;
% Parameters of the initial field
% rng('default'); % For reproducibility
g = func_sum_normal(m,n,mu,stdev,K); whos g; % double
% figure(500), imshow(g,[]), title('NormalNoise');
figure(501); imagesc(g); colorbar;
STEP = 6 % FILTERING with 2D-Filter
sigma = sigma % remind sigma value
z = conv2(g,w,'same'); % 2-D Filter Sample Output
z1 = mat2gray(z); mz1 = mean(z1); % row vector
z0 = zeros(m,n);
for i = 1:m
for j = 1:n
z0(i,j) = z1(i,j) - mz1(j);
end;
end;
figure(600), imshow(z0,[]), title('2-D Filter Output')
figure(601); imagesc(z0); colorbar, title('2-D Filter Output')
% ts = input('STOP6','s');
STEP = 7
r = corrcoef(z0); % SampleCorrelationCoeff r
nr = length(r) % 511
% columns of the field z represent random variables
% rows represent observations
figure(701), imshow(r,[]), title('Sample Correlation Coeff r for z')
figure(702); imagesc(r), colorbar,
title('Sample Correlation Coeff r for z')
figure(703), mesh(r), title('Sample Correlation Coeff r for z')
c = zeros(1,n); % 1 256
J0 = floor(n/2) - L0; % 241
for j = 1:L %
c(j+J0) = 1;
CM(X4,j) = 1;
end;
X7 = 128;
figure(704), plot(1:n,r(X7,1:n)), grid
title('Sample Correlation Coeff r')
hold on
plot(1:n,c)
hold off
% ts = input('STOP7','s');
STEP = 8 % Sample Power Spectrum Pr for r
r1 = zeros(n);
for i = 1:n
for j = 1:n
r1(i,j) = r(i,j)*(-1)^(i+j);
end;
end;
Pr = abs(fft2(r1)); % Sample Power Spectrum
figure(800), imshow(Pr,[]), title('Sample Power Spectrum Pr for r')
figure(801); imagesc(Pr), colorbar,
title('Sample Power Spectrum Pr for r')
figure(802), mesh(Pr), title('Sample Power Spectrum Pr for r')
%%%%%%%%%%%%%%%%%%%%%%%%%%
p = zeros(1,n); %
J0 = floor(n/2) - L0; %
for j = 1:L % 101
p(j+J0) = 1;
PM(X3,j) = 1;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%
X8 = 128;
PrMAX = max(Pr(X8,:));
figure(803), plot(1:n,Pr(X8,1:n)/PrMAX), grid
title('SamplePowerSpect Pr')
hold on
plot(1:n,p)
hold off
% ts = input('STOP8','s');
STEP = 9 %Sample Power Spectrum Pz for z
z1 = zeros(n);
for i = 1:n
for j = 1:n
z1(i,j) = z(i,j)*(-1)^(i+j);
end;
end;
Z = abs(fft2(z1)); % Sample Spectrum of z
%%%%%%%%%%%%%%%%%
Pz = Z.^2;
figure(900), imshow(Pz,[]), title('Sample Power Spectrum Pz for z')
figure(901); imagesc(Pz), colorbar,
title('Sample Power Spectrum Pz for z')
figure(902), mesh(Pz), title('Sample Power Spectrum Pz for z')
%%%%%%%%%%%%%%%%%%%%
X9 = 128;
PzMAX = max(Pz(X9,:));
figure(903), plot(1:n,Pz(X9,1:n)/PzMAX), grid
title('SamplePowerSpect Pz')
hold on
plot(1:n,p)
hold off
STEP = 10 % Correlation C1 averaged by the rows
[c,lags] = xcorr(z0);% size(c) %511 65536
nc = length(lags) % 511
% figure(1000), plot(1:nc,c(:,250)), grid
%%%%%%%%%%%%%%%%%
C1 = zeros(1,nc);
y = zeros(1,n);
for i = 1:m
for j = 1:n
y(j) = z0(i,j);
end;
[c1,lags] = xcorr(y); % Sample Correlation on the i-th row
C1 = ((i-1)/i)*C1 + (1/i)*c1;
end;
C1MAX = max(C1(:));
% figure(1001), stem(lags,C1(:)/C1MAX), grid
% title('Correlation C1 averaged by the rows')
c1 = zeros(nc,1); %
J0 = floor(nc/2) - L0; % 240
for j = 1:L %
c1(j+J0) = CM(X4,j);
end;
figure(1002), plot(1:nc,C1(:)/C1MAX), grid
title('Correlation Coeff C1')
hold on
plot(1:nc,c1)
hold off
% ts = input('STOP10','s');
STEP = 11 % Sample Power Spectrum Pc for C1
for j = 1:nc
C11(j) = C1(j)*(-1)^j;
end;
Pc = abs(fft(C11)); % size(Pc) % 511
Pc = Pc.^2;
figure(1100), plot(1:nc,Pc), grid
title('Sample Power Spectrum Pc for C1')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
p1 = zeros(nc,1);
J0 = floor(nc/2) - L0; %
for j = 1:L % 101
p1(j+J0) = PM(X3,j);
end;
PcMAX = max(Pc(:));
% x = 200:300;
x = 1:nc;
figure(1101), plot(x,Pc(x)/PcMAX), grid
title('SamplePowerSpect Pc')
hold on
plot(x,p1(x))
hold off
close all
clear all
clc
STEP = 1 % 2-D Filter Mask
sigma = 20
ssq = sigma^2;
r = 1/(2*ssq);
GaussianDieOff = .0001;
% Design the filters - a gaussian
pw = 1:30; % possible widths
width = find(exp(-(pw.*pw)*r)>GaussianDieOff,1,'last')
if isempty(width), width = 1;end
% the user entered a really small sigma
L = width % Size of the window [Samples]
L0 = floor(L/2)
w = zeros(L);
for i = 1:L
for j = 1:L
w(i,j) = exp(-((j-L0)^2+(i-L0)^2)/(4*ssq));
end;
end;
figure(100), imshow(w), title('Gaussian')
figure(101), mesh(w), title('Gaussian')
STEP = 2
w1 = zeros(L,1);
for i = 1:L
for j = 1:L
w1(i,j) = w(i,j)*(-1)^(i+j);
end;
end;
W = abs(fft2(w1)); size(W) % Fourier Transform
STEP = 3
P = W.^2; % Power Spectrum of Gauss Window
X3 = L0;
PMAX = max(P(X3,:));
figure(302), plot(1:L,P(X3,1:L)/PMAX), grid
title('Power Transmission Coeff P')
ylabel('P(f)')
STEP = 4 % Inverse Transform
P1 = zeros(L);
for i = 1:L
for j = 1:L
P1(i,j) = P(i,j)*(-1)^(i+j);
end;
end;
C = abs(ifft2(P1)); % Inverse Fourier on Power Spectrum
X4 = L0;
CMAX = max(C(X4,:));
figure(402), plot(1:L,C(X4,1:L)/CMAX),
title('Normalized Correlation C'), grid
ts = input('STOP4','s');
STEP = 5 %NormalNoise
m = 256; n = 256;
K = 1 % number of fields
% NORMAL FIELD
mu = 30; stdev = 30;
% Parameters of the initial field
% rng('default'); % For reproducibility
g = func_sum_normal(m,n,mu,stdev,K); whos g; % double
% figure(500), imshow(g,[]), title('NormalNoise');
figure(501); imagesc(g); colorbar;
STEP = 6 % FILTERING with 2D-Filter
sigma = sigma % remind sigma value
z = conv2(g,w,'same'); % 2-D Filter Sample Output
z1 = mat2gray(z); mz1 = mean(z1); % row vector
z0 = zeros(m,n);
for i = 1:m
for j = 1:n
z0(i,j) = z1(i,j) - mz1(j);
end;
end;
figure(600), imshow(z0,[]), title('2-D Filter Output')
figure(601); imagesc(z0); colorbar, title('2-D Filter Output')
% ts = input('STOP6','s');
STEP = 7
r = corrcoef(z0); % SampleCorrelationCoeff r
nr = length(r) % 511
% columns of the field z represent random variables
% rows represent observations
figure(701), imshow(r,[]), title('Sample Correlation Coeff r for z')
figure(702); imagesc(r), colorbar,
title('Sample Correlation Coeff r for z')
figure(703), mesh(r), title('Sample Correlation Coeff r for z')
c = zeros(1,n); % 1 256
J0 = floor(n/2) - L0; % 241
for j = 1:L %
c(j+J0) = 1;
CM(X4,j) = 1;
end;
X7 = 128;
figure(704), plot(1:n,r(X7,1:n)), grid
title('Sample Correlation Coeff r')
hold on
plot(1:n,c)
hold off
% ts = input('STOP7','s');
STEP = 8 % Sample Power Spectrum Pr for r
r1 = zeros(n);
for i = 1:n
for j = 1:n
r1(i,j) = r(i,j)*(-1)^(i+j);
end;
end;
Pr = abs(fft2(r1)); % Sample Power Spectrum
figure(800), imshow(Pr,[]), title('Sample Power Spectrum Pr for r')
figure(801); imagesc(Pr), colorbar,
title('Sample Power Spectrum Pr for r')
figure(802), mesh(Pr), title('Sample Power Spectrum Pr for r')
%%%%%%%%%%%%%%%%%%%%%%%%%%
p = zeros(1,n); %
J0 = floor(n/2) - L0; %
for j = 1:L % 101
p(j+J0) = 1;
PM(X3,j) = 1;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%
X8 = 128;
PrMAX = max(Pr(X8,:));
figure(803), plot(1:n,Pr(X8,1:n)/PrMAX), grid
title('SamplePowerSpect Pr')
hold on
plot(1:n,p)
hold off
% ts = input('STOP8','s');
STEP = 9 %Sample Power Spectrum Pz for z
z1 = zeros(n);
for i = 1:n
for j = 1:n
z1(i,j) = z(i,j)*(-1)^(i+j);
end;
end;
Z = abs(fft2(z1)); % Sample Spectrum of z
%%%%%%%%%%%%%%%%%
Pz = Z.^2;
figure(900), imshow(Pz,[]), title('Sample Power Spectrum Pz for z')
figure(901); imagesc(Pz), colorbar,
title('Sample Power Spectrum Pz for z')
figure(902), mesh(Pz), title('Sample Power Spectrum Pz for z')
%%%%%%%%%%%%%%%%%%%%
X9 = 128;
PzMAX = max(Pz(X9,:));
figure(903), plot(1:n,Pz(X9,1:n)/PzMAX), grid
title('SamplePowerSpect Pz')
hold on
plot(1:n,p)
hold off
STEP = 10 % Correlation C1 averaged by the rows
[c,lags] = xcorr(z0);% size(c) %511 65536
nc = length(lags) % 511
% figure(1000), plot(1:nc,c(:,250)), grid
%%%%%%%%%%%%%%%%%
C1 = zeros(1,nc);
y = zeros(1,n);
for i = 1:m
for j = 1:n
y(j) = z0(i,j);
end;
[c1,lags] = xcorr(y); % Sample Correlation on the i-th row
C1 = ((i-1)/i)*C1 + (1/i)*c1;
end;
C1MAX = max(C1(:));
% figure(1001), stem(lags,C1(:)/C1MAX), grid
% title('Correlation C1 averaged by the rows')
c1 = zeros(nc,1); %
J0 = floor(nc/2) - L0; % 240
for j = 1:L %
c1(j+J0) = CM(X4,j);
end;
figure(1002), plot(1:nc,C1(:)/C1MAX), grid
title('Correlation Coeff C1')
hold on
plot(1:nc,c1)
hold off
% ts = input('STOP10','s');
STEP = 11 % Sample Power Spectrum Pc for C1
for j = 1:nc
C11(j) = C1(j)*(-1)^j;
end;
Pc = abs(fft(C11)); % size(Pc) % 511
Pc = Pc.^2;
figure(1100), plot(1:nc,Pc), grid
title('Sample Power Spectrum Pc for C1')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
p1 = zeros(nc,1);
J0 = floor(nc/2) - L0; %
for j = 1:L % 101
p1(j+J0) = PM(X3,j);
end;
PcMAX = max(Pc(:));
% x = 200:300;
x = 1:nc;
figure(1101), plot(x,Pc(x)/PcMAX), grid
title('SamplePowerSpect Pc')
hold on
plot(x,p1(x))
hold off
Соседние файлы в папке lab2 вроде