问题:”当用作索引时,冒号运算符需要整数操作数”,急求帮忙看看以下错误程序?
279 views (last 30 days)
Show older comments
腾龙娱乐公司客服17787482287
on 17 Apr 2022
Answered: starlight Smith
on 17 Apr 2022
程序运行后出现:

程序:[y,Fs]=audioread('handel.wav');sound(y,Fs);
n=length(y);
y_p=fft(y,n);
f=fs*(0:n/2-1)/n;
figure(1)
subplot(2,1,1);
plot(y);
title(' 原始语音信号采样后的时域波形 ');
xlabel(' 时间轴');
ylabel(' 幅值A');
subplot(2,1,2);
plot(f,abs(y_p(1:n/2)));
title(' 原始语音信号采样后的频谱图 ');
xlabel(' 频率Hz');
ylabel(' 频率幅值');
L=length(y);
noise=0.1*randn(L,2);
y_z=y+noise;
sound(y_z,n);
f=fs*(0:n/2-1)/n;
figure(2)
subplot(2,1,1);
plot(y_z);
title(' 加噪语音信号时域波形');
xlabel(' 时间轴');
ylabel(' 幅值A');
subplot(2,1,2);
plot(f,abs(y_p(1:n/2)));
title(' 加噪语音信号频谱图');
xlabel(' 频率Hz');
ylabel(' 频率幅值');
0 Comments
Accepted Answer
starlight Smith
on 17 Apr 2022
这是警告,不是报错。比如
M = 1 : 1 : 5;
n = length( M );
M( 1 : n/2 )
这样的代码也会出警告,但实际上能够运行。无非就是 n 是奇数,导致 n/2 不是整数,M( 1 : n/2 )这种索引调用,实际上调用的是M( 1 : floor( n/2 ) ),因此MATLAB才会给出警告,让使用者明白,floor( n/2 )可能会导致不符合代码编写目的的情形,需要注意。
0 Comments
More Answers (0)
See Also
Categories
Find more on Pulsed Waveforms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!