首页 新闻 会员 周边 捐助

新手对信号处理的matlab代码进行调试,求帮助

0
[已关闭问题] 关闭于 2024-09-27 16:44

matlab代码运行过程中一直提示索引不能超过9000,经调试本人能看出的修改地方已经经过修改,但是还是出现,想请问各位这个代码那里需要进行修改才能解决这个问题

%%%%%%%%% the signal with fast oscillating IF %%%%%%%%%%%%%
% Chen S, Yang Y, Peng Z, et al, Detection of Rub-Impact Fault for Rotor-Stator Systems: A Novel Method Based on Adaptive Chirp Mode Decomposition, Journal of Sound and Vibration, 2018.
% instantaneous frequency initialization by finding peaks of Fourier spectrum
clc
clear
close all

% 提示用户输入信号文件的路径
filename = input('请输入信号文件的完整路径:', 's');

% 检查文件是否存在
if exist(filename, 'file')
% 文件存在,读取信号
Sig = load(filename);
if length(size(Sig)) > 1
Sig = Sig(:, 1); % 假设信号在第一列
end

% 根据信号的实际长度重新生成时间向量
t = linspace(0, 1, length(Sig));

else
error('文件不存在,请检查路径是否正确。');
end

% 绘制原始信号
figure
set(gcf,'Position',[20 100 640 500]);
set(gcf,'Color','w');
plot(t,Sig,'linewidth',2);
xlabel('Time / Sec','FontSize',24,'FontName','Times New Roman');
ylabel('Amplitude / AU','FontSize',24,'FontName','Times New Roman');
set(gca,'FontSize',24)
set(gca,'linewidth',2);

%% Fourier spectrum
Spec = 2abs(fft(Sig))/length(Sig);
Spec = Spec(1:round(end/2));
Freqbin = linspace(0, 1000/2, length(Spec));
figure
set(gcf,'Position',[20 100 640 500]);
set(gcf,'Color','w');
plot(Freqbin, Spec, 'linewidth', 2);
xlabel('Frequency / Hz','FontSize',24,'FontName','Times New Roman');
ylabel('Amplitude / AU','FontSize',24,'FontName','Times New Roman');
set(gca,'FontSize',24)
set(gca,'linewidth',2);
axis([100 500 0 1.1
max(Spec)])

%% STFT
% 设置 STFT 参数
window = hamming(min(512, length(Sig))); % 使用较小的窗口大小
noverlap = round(min(256, length(Sig) / 2)); % 确保重叠不是信号的一半以上
nfft = min(512, length(Sig)); % 使用较小的 FFT 长度

% 调用 STFT 函数
[TFSpec, f, t_stft] = stft(Sig, 'Window', window, 'OverlapLength', noverlap, 'FFTLength', nfft);

% 绘制 STFT 结果
figure
imagesc(t_stft, f, abs(TFSpec));
axis xy;
axis([0 max(t_stft) 0 max(f)]);
set(gcf,'Position',[20 100 320 250]);
xlabel('Time / Sec','FontSize',12,'FontName','Times New Roman');
ylabel('Frequency / Hz','FontSize',12,'FontName','Times New Roman');
set(gca,'YDir','normal')
set(gca,'FontSize',12);
set(gcf,'Color','w');

%% parameter setting
alpha0 = 1e-3;
beta = 1e-4;
tol = 1e-8;

%% Component 1 extraction
[~, findex1] = max(Spec);
peakfre1 = Freqbin(findex1);
iniIF1 = peakfre1 * ones(1, length(Sig));

[IFest1, IAest1, sest1] = ACMD(Sig, 1000, iniIF1, alpha0, beta, tol);

%% Component 2 extraction
residue1 = Sig - sest1;
resSpec = 2 * abs(fft(residue1)) / length(residue1);
resSpec = resSpec(1:round(end/2));
[~, findex2] = max(resSpec);
peakfre2 = Freqbin(findex2);
iniIF2 = peakfre2 * ones(1, length(Sig));

[IFest2, IAest2, sest2] = ACMD(residue1, 1000, iniIF2, alpha0, beta, tol);

%% estimated IF
figure
plot(t, IFest1, 'r--', 'linewidth', 3);
hold on;
plot(t, IFest2, 'b--', 'linewidth', 3);
set(gcf,'Position',[20 100 640 500]);
xlabel('Time / Sec','FontSize',24,'FontName','Times New Roman');
ylabel('Frequency / Hz','FontSize',24,'FontName','Times New Roman');
set(gca,'YDir','normal')
set(gca,'FontSize',24);
set(gca,'linewidth',2);
set(gca,'FontName','Times New Roman')
set(gcf,'Color','w');
axis([0 1 100 500]);

%% Reconstructed modes
figure
set(gcf,'Position',[20 100 640 500]);
subplot(2,1,1)
set(gcf,'Color','w');
plot(t,sest1,'b--','linewidth',2);
ylabel('C1','FontSize',24,'FontName','Times New Roman');
set(gca,'YDir','normal')
set(gca,'FontSize',24);
set(gca,'linewidth',2);
axis([0 1 -1 1])
subplot(2,1,2)
set(gcf,'Color','w');
plot(t,sest2,'b--','linewidth',2);
ylabel('C2','FontSize',24,'FontName','Times New Roman');
xlabel('Time / Sec','FontSize',24,'FontName','Times New Roman')
set(gca,'YDir','normal')
set(gca,'FontSize',24);
set(gca,'linewidth',2);
axis([0 1 -1 1])

%% adaptive time-frequency spectrum
band = [0 500];
IFmulti = [IFest1; IFest2];
IAmulti = [IAest1; IAest2];
[ASpec, fbin] = TFspec(IFmulti, IAmulti, band);
figure
imagesc(t, fbin, abs(ASpec));
axis([0 1 100 500]);
set(gcf,'Position',[20 100 640 500]);
xlabel('Time / Sec','FontSize',24,'FontName','Times New Roman');
ylabel('Frequency / Hz','FontSize',24,'FontName','Times New Roman');
set(gca,'YDir','normal')
set(gca,'FontSize',24);
set(gcf,'Color','w');
colorbar('YTickLabel',{' '})

% 计算并显示SNR
SNR1 = SNR(Sig, sest1);
SNR2 = SNR(Sig, sest2);
disp(['SNR for Component 1: ', num2str(SNR1)]);
disp(['SNR for Component 2: ', num2str(SNR2)]);

报错原因:索引超过数组元素的数量。索引不能超过 9000。

11010101的主页 11010101 | 菜鸟二级 | 园豆:206
提问于:2024-09-27 10:48
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册