gpt4 book ai didi

matlab - 在matlab中更改过滤器(B,A,X)和内存不足错误

转载 作者:行者123 更新时间:2023-12-04 18:16:25 24 4
gpt4 key购买 nike

这篇文章与我之前的问题有关:image processing in matlab
因为我在那里上传了我的算法。
我的想法是我正在尝试更改代码的过滤部分。
在 matlab filter.m 函数中可以接受过滤器(B,A,我的像素随时间演变),它会返回过滤后的值。
但目前我必须一起通过整个时间序列。

但问题是现在我想以某种方式更改代码而不是将整个时间序列传递给过滤器,我想一次传递一个值,但我希望过滤器函数将值视为第 n 个值而不是第一个值。
我创建了一个 sudo 代码,因为我在代码中注入(inject)了一张图片,但我不知道如何更改过滤部分。任何人有任何想法吗?

clear all
j=1;
for i=0:3000
str = num2str(i);
str1 = strcat(str,'.mat');
load(str1);
D{j}=A(20:200,130:230);
j=j+1;
end
N=5;
w = [0.00000002 0.05;0.05 0.1;0.1 0.15;0.15 0.20;
0.20 0.25;0.25 0.30;0.30 0.35;0.35 0.40;
0.40 0.45;0.45 0.50;0.50 0.55;0.55 0.60;
0.60 0.65;0.65 0.70;0.70 0.75;0.75 0.80;
0.80 0.85;0.85 0.90;0.90 0.95;0.95 0.99999999];
for ind=1:20
wn = w(ind,:);
[b,a] = butter(N,wn);
bCoeff(ind,:)=b;
aCoeff(ind,:)=a;
end

ts=[];
sumLastImages=[];
for k=1:10 %number of images
for bands=1:20 %number of bands
for i=1:10 %image h
for j=1:10 %image w
pixelValue = D{k}(i,j);
% reflectivity elimination
% for the current pixel, have the summation of the same position from before
% images and create a mean value base on the temporal values
sumLastImages(i,j)=pixelValue+sumLastImages(i,j);
meanValue = sumLastImages(i,j)/k;

if(meanValue==0)
filteredimages{bands}(i,j)=0;
continue;
else
pixel_calculated_meanvalue = pixelValue/meanValue;
end
% filter part that need to be changed, and because we are adding
% one value then the reutrn of the filter is one too
ts_f = filter(bCoeff(bands,:), aCoeff(bands,:), ...
pixel_calculated_meanvalue);
filteredimages{bands}(i,j)=ts_f;
end
end

finalImagesSummation{bands}(:,:) = ...
(filteredimages{bands}(:,:)^2)+finalImagesSummation{bands}(:,:);
finalImages{bands}(:,:)=finalImagesSummation/k;
end
end

编辑
我设法更改了这样的代码,现在我加载了第 200 张图像,之后我可以将图像一张一张地注入(inject)过滤器,但现在的问题是我得到了 "Out of memory. Type HELP MEMORY for your options."大错误
图片。
这是我的代码任何有效代码的想法:
%%
cd('D:\MatlabTest\06-06-Lentils');
clear all

%%

N=5;
W = [0.0 0.10;0.10 0.20;0.20 0.30;0.30 0.40;
0.40 0.50;0.50 0.60 ;0.60 0.70;0.70 0.80 ;
0.80 0.90;0.90 1.0];

[bCoeff{1},aCoeff{1}] = butter(N,0.1,'Low');
for ind=2:9
wn = W(ind,:);
[b,a] = butter(N,wn);
bCoeff{ind}=b;
aCoeff{ind}=a;
end
[bCoeff{10},aCoeff{10}] = butter(N,0.9,'high');

%%
j=1;

D = zeros(200,380,320);

T = 200;
K = 0:399;
l = T+1;
Yout = cell(1,10);
figure;

for i = 1:length(K)-200
disp(i)

if i == 1
for j = 1:T
str = int2str(K(1)+j-1);
str1 = strcat(str,'.mat');
load(str1);
D(j,1:380,1:320) = A;
end

else

str = int2str(l);
str1 = strcat(str,'.mat');
load(str1);

temp = D(2:200,1:380,1:320) ;
temp(200,1:380,1:320) = A;
D = temp;
clear temp
l = l +1;

end


for p = 1:380
for q = 1:320
x = D(:,p,q) - mean(D(:,p,q));

for k = 1:10
temp = filter(bCoeff{k},aCoeff{k},x);
if i == 1
Yout{k}(p,q) = mean(temp);
else
Yout{k}(p,q) = (Yout{k}(p,q) + mean(temp))/2;
end
end
end
end

for k = 1:10
subplot(5,2,k)
subimage(Yout{k}*1000,[0 255]);
color bar
colormap jet
end
pause(0.01);
end

disp('Done Loading...')

最佳答案

无需重写过滤功能,有一个简单的解决方案!

如果你想喂 filter 一次一个 sample ,您需要通过 状态参数同样,每个输入样本都根据其前身进行处理。过滤后,新的状态实际上是作为第二个参数返回的,这样大部分工作已经由 MATLAB 为您完成。 这是个好消息!

为了便于阅读,请允许我用简单的字母临时替换您的变量名称:

a = aCoeff(bands, :);    
b = bCoeff(bands, :);
x = pixel_calculated_meanvalue;
ts_fy 表示.

所以,这个:
y = filter(b, a, x);

实际上等价于:
N = numel(x);
y = zeros(N, 1); %# Preallocate memory for output
z = zeros(max(length(a), length(b)) - 1, 1); %# This is the initial filter state
for i = 1:N
[y(i), z] = filter(b, a, x(i), z);
end

您可以自己检查结果是否相同!

对于您的示例,代码将是:
N = numel(pixel_calculated_meanvalue);
ts_f = zeros(N, 1);
z = zeros(max(length(aCoeff(bands, :)), length(bCoeff(bands, :))) - 1, 1);
for i = 1:N
[ts_f(i), z] = filter(bCoeff(bands, :), aCoeff(bands, :), ...
pixel_calculated_meanvalue(i), z);
end

使用这种方法,您可以一次处理一个输入样本,只需确保在每个 filter 之后存储最后一个过滤器状态即可。称呼。如果您计划使用多个过滤器,则必须为每个过滤器存储一个状态向量!

关于matlab - 在matlab中更改过滤器(B,A,X)和内存不足错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11583523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com