优化下面代码,使得输入的数字开头几个0,输出的开头也要有几个0:tm = [1, 2 ,3, 65; 4, 5, 6, 66; 7, 8, 9, 67; 42, 0, 35, 68]; N = 205; K = [18, 20, 22, 24, 31, 34, 38, 42]; f1 = [697, 770, 852, 941]; f2 = [1209, 1336, 1477, 1633]; TN0 = input('输入6个号码:','s'); TN = str2num(TN0); len = length(TN0); TNr = 0; first_flag = 0; if len == 6 %判断是否为6为号码,不为6位则报错输出 for m=1:6 d = fix(TN/10^(6-m)); TN = TN-d10^(6-m); for p=1:4 for q=1:4 if tm(p,q)==abs(d);break,end end if tm(p,q)==abs(d);break,end end n = 0:1023; x = sin(2pinf1(p)/8000)+sin(2pin*f2(q)/8000); sound(x, 8000); pause(0.1) X=goertzel(x(1:N),K+1); val = abs(X); subplot(3, 2, m);stem(K, val, '.');grid;xlabel('k');ylabel('|X(k)|'); axis([10 50 0 120]); limit = 80; for s=5:8 if val(s)>limit,break,end end for r=1:4 if val(r)>limit,break,end end TNr=TNr+tm(r,s-4)*10^(6-m); end disp('接收端检测到的号码为') disp(TNr) else disp("ERROR:输入号码不为6位"); end
时间: 2023-08-24 12:06:18 浏览: 42
tm = [1, 2 ,3, 65; 4, 5, 6, 66; 7, 8, 9, 67; 42, 0, 35, 68];
N = 205;
K = [18, 20, 22, 24, 31, 34, 38, 42];
f1 = [697, 770, 852, 941];
f2 = [1209, 1336, 1477, 1633];
TN0 = input('输入6个号码:','s');
TN = str2num(TN0);
len = length(TN0);
TNr = 0;
first_flag = 0;
if len == 6
% 判断是否为6为号码,不为6位则报错输出
for m=1:6
d = fix(TN/10^(6-m));
TN = TN-d*10^(6-m);
for p=1:4
for q=1:4
if tm(p,q)==abs(d)
break
end
end
if tm(p,q)==abs(d)
break
end
end
n = 0:1023;
x = sin(2*pi*f1(p)/8000)+sin(2*pi*f2(q)/8000);
sound(x, 8000);
pause(0.1)
X=goertzel(x(1:N),K+1);
val = abs(X);
subplot(3, 2, m);
stem(K, val, '.');
grid;
xlabel('k');
ylabel('|X(k)|');
axis([10 50 0 120]);
limit = 80;
s_idx = find(val(5:8)>limit, 1);
r_idx = find(val(1:4)>limit, 1);
if isempty(s_idx)
s_idx = 5;
else
s_idx = s_idx + 4;
end
if isempty(r_idx)
r_idx = 1;
end
TNr = TNr + tm(r_idx, s_idx-4)*10^(6-m);
end
% 输出号码,前面补0
TNr_str = num2str(TNr);
zero_num = 6 - length(TNr_str);
zero_str = repmat('0', 1, zero_num);
disp(['接收端检测到的号码为 ', zero_str, TNr_str]);
else
disp("ERROR:输入号码不为6位");
end
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)