T1 = 0.2; T2 = 0.9; for i = 1:m for j = 1:n x = squeeze(data(i, j, :)); y = V' * x; z = zeros(p, 1); for k = 1:p if y(k) <= T1 z(k) = 0; elseif y(k) >= T2 z(k) = 1; else z(k) = (y(k) - T1) / (T2 - T1); end end data(i, j, :) = V * z; end end
时间: 2023-06-15 08:05:28 浏览: 67
这段代码使用了一个阈值函数,将数据从原始空间映射到另一个空间。具体来说,它将一个大小为$m\times n\times p$的三维数据张量(即data)中的每个元素向量(大小为p)从原始空间映射到一个大小为p的向量空间中。这里使用了一个投影矩阵V,将p维向量映射到一个更低维度的空间中。然后,对于每个映射后的向量y,通过阈值函数将其映射到一个二元向量z中。具体来说,如果y的某个元素小于T1,则z中对应的元素为0;如果y的某个元素大于T2,则z中对应的元素为1;否则,z中对应的元素为一个介于0和1之间的值,这个值是通过线性插值计算得到的。最后,使用投影矩阵的逆矩阵V将新的二元向量z映射回原始空间,得到新的数据张量data。
相关问题
traceH = hilbert(trace); clear trace traceH = traceH.*conj(traceH(:,1)); GAPL = 0.4; moveN = round(8/GAPL); [output] = moveVectorMean2(traceH,moveN); traceH=output; clear output amp = abs(traceH); [M,N] = size(amp); gapD = round(4/GAPL); [output] = spacephsed(traceH,gapD); clear traceH phaOutput = angle(output); clear output phaseUnw(:,:) = unwrap(squeeze(phaOutput(:,:))')'; clear phaOutput [~,axisTp,outputtp] = denoiseFunc(5,500,phaseUnw,fd,fs,GAPL); % 慢轴,快轴 clear phaseUnw [~,axisT,outputint] = denoiseFunc(5,490,amp,fd,fs,GAPL); % 慢轴,快轴 clear amp for i=1:1:floor(length(outputtp(:,1))/10) phase(i,:)=mean(outputtp((i-1)*10+1:i*10,:)); end clear outputtp for i=1:1:floor(length(outputint(:,1))/10) intensity(i,:)=mean(outputint((i-1)*10+1:i*10,:)); end clear outputint figure for i=1:1:22 plot(axisTp,phase(i,:)/10*10+i*4); hold on end hold off figure for i=1:1:22 plot(axisT,intensity(i,:)/10*10+i*4); hold on end hold off % for i=1:1:floor(length(intensity(1,:))/4000) % stdp(:,i)=var(phase(:,(i-1)*4000+1:i*4000),0,2); % stdt(:,i)=var(intensity(:,(i-1)*4000+1:i*4000),0,2); % end % for i=1:1:29 % positionp(i)=find(stdp(:,i)==max(stdp(:,i))); % positioni(i)=find(stdt(:,i)==max(stdt(:,i))); % end %%将数据切为不同帧 for i=1:1:10 trandatap(:,:,i)=phase(1:8,(i-1)*4000+1:(i)*4000); trandatai(:,:,i)=intensity(1:8,(i-1)*4000+1:(i)*4000); end %%删除信噪比较差的数据。(先把数据图像画图,然后筛选) trainphase=trandatap; traintensity=trandatai; trainphase(:,:,27:29)=[]; trainphase(:,:,1:2)=[]; trainphase(:,:,16)=[]; trainphase(:,:,14)=[]; trainphase(:,:,10)=[]; trainphase(:,:,7:8)=[]; trainphase(:,:,4)=[]; trainphase(:,:,1)=[]; traintensity(:,:,27:29)=[]; traintensity(:,:,1:2)=[]; traintensity(:,:,16)=[]; traintensity(:,:,14)=[]; traintensity(:,:,10)=[]; traintensity(:,:,7:8)=[]; traintensity(:,:,4)=[]; traintensity(:,:,1)=[];
traceH = hilbert(trace);
clear trace
traceH = traceH.*conj(traceH(:,1));
GAPL = 0.4;
moveN = round(8/GAPL);
[output] = moveVectorMean2(traceH,moveN);
traceH=output;
clear output
amp = abs(traceH);
[M,N] = size(amp);
gapD = round(4/GAPL);
[output] = spacephsed(traceH,gapD);
clear traceH
phaOutput = angle(output);
clear output
phaseUnw(:,:) = unwrap(squeeze(phaOutput(:,:))')';
clear phaOutput
[~,axisTp,outputtp] = denoiseFunc(5,500,phaseUnw,fd,fs,GAPL); % Slow axis, Fast axis
clear phaseUnw
[~,axisT,outputint] = denoiseFunc(5,490,amp,fd,fs,GAPL); % Slow axis, Fast axis
clear amp
for i=1:1:floor(length(outputtp(:,1))/10)
phase(i,:)=mean(outputtp((i-1)*10+1:i*10,:));
end
clear outputtp
for i=1:1:floor(length(outputint(:,1))/10)
intensity(i,:)=mean(outputint((i-1)*10+1:i*10,:));
end
clear outputint
figure
for i=1:1:22
plot(axisTp,phase(i,:)/10*10+i*4);
hold on
end
hold off
figure
for i=1:1:22
plot(axisT,intensity(i,:)/10*10+i*4);
hold on
end
hold off
% for i=1:1:floor(length(intensity(1,:))/4000)
% stdp(:,i)=var(phase(:,(i-1)*4000+1:i*4000),0,2);
% stdt(:,i)=var(intensity(:,(i-1)*4000+1:i*4000),0,2);
% end
% for i=1:1:29
% positionp(i)=find(stdp(:,i)==max(stdp(:,i)));
% positioni(i)=find(stdt(:,i)==max(stdt(:,i)));
% end
%% Split the data into different frames
for i=1:1:10
trandatap(:,:,i)=phase(1:8,(i-1)*4000+1:(i)*4000);
trandatai(:,:,i)=intensity(1:8,(i-1)*4000+1:(i)*4000);
end
%% Remove data with poor signal-to-noise ratio (plot the data and then select)
trainphase=trandatap;
traintensity=trandatai;
trainphase(:,:,27:29)=[];
trainphase(:,:,1:2)=[];
trainphase(:,:,16)=[];
trainphase(:,:,14)=[];
trainphase(:,:,10)=[];
trainphase(:,:,7:8)=[];
trainphase(:,:,4)=[];
trainphase(:,:,1)=[];
traintensity(:,:,27:29)=[];
traintensity(:,:,1:2)=[];
traintensity(:,:,16)=[];
traintensity(:,:,14)=[];
traintensity(:,:,10)=[];
traintensity(:,:,7:8)=[];
traintensity(:,:,4)=[];
traintensity(:,:,1)=[];
def residual_network(inputs, dropout_rate=0.1): # 第一层卷积层 x = Conv1D(64, 3, padding="same")(inputs) x = BatchNormalization()(x) x = Activation("relu")(x) # 第二层卷积层 x = Conv1D(64, 3, padding="same")(x) x = BatchNormalization()(x) x = Activation("relu")(x) # 残差块 for i in range(5): y = Conv1D(64, 3, padding="same")(x) y = BatchNormalization()(y) y = Activation("relu")(y) y = Conv1D(64, 3, padding="same")(y) y = BatchNormalization()(y) y = Add()([x, y]) x = Activation("relu")(y) x = Dropout(dropout_rate)(x) # 全局池化层和全连接层 x = Flatten()(x) x = Dense(128, activation="relu")(x) x = Dropout(dropout_rate)(x) x = Dense(3, activation="linear")(x) outputs = x return outputs # 构建模型 inputs = Input(shape=input_shape) outputs = residual_network(inputs) model = Model(inputs=inputs, outputs=outputs) # 编译模型 model.compile(loss="mean_squared_error", optimizer=Adam()) # 定义EarlyStopping回调函数 early_stopping = EarlyStopping(monitor='val_loss', patience=10, verbose=1, mode='min') # 训练模型 history = model.fit(data[..., np.newaxis], data, epochs=100, validation_split=0.2, callbacks=[early_stopping]) # 预测数据 predicted_data = model.predict(data[..., np.newaxis]) predicted_data = np.squeeze(predicted_data) # 可视化去噪前后的数据 fig, axs = plt.subplots(3, 1, figsize=(12, 8)) for i in range(3): axs[i].plot(data[:, i], label="Original Signal") axs[i].plot(predicted_data[:, i], label="Denoised Signal") axs[i].legend() plt.savefig("denoised_signal_DRN.png")
这段代码是一个使用残差网络(ResNet)对信号进行降噪的实现。主要包括构建模型、编译模型、训练模型以及预测数据等步骤。
首先,定义了一个名为`residual_network`的函数,用于构建残差网络模型。该模型包括卷积层、批归一化层、激活函数层、残差块、全局池化层和全连接层等多个层级,其中最关键的是残差块,它允许信息在网络中进行跳跃连接,有效地缓解了信息在网络中逐层传递时的信息丢失问题。
其次,通过`Input`函数定义输入层,然后将输入层作为输入传入`residual_network`函数中,得到输出层。再通过`Model`函数将输入层和输出层组合成完整的模型,用`compile`函数编译模型,定义损失函数和优化器。接着,使用`fit`函数训练模型,并通过`EarlyStopping`回调函数提前停止训练,避免模型过拟合。最后,使用训练好的模型对数据进行预测,并将预测结果可视化出来。
需要注意的是,这里使用的是`Conv1D`函数,表示一维卷积层,因为输入数据是一维信号。另外,`Dropout`函数用于在训练过程中随机断开一定比例的神经元,以减少过拟合。
阅读全文