我遇到了在Matlab中跨'case'语句共享变量的问题。 我正在编码梳状滤波器,它会延迟音频文件。
以下是整个代码:
%Initial valuses of RT and Tc
Tc = 0.02;
RT = 0.5;
fs = 44100;
Ts = 1/fs;
M = Tc/Ts;
g = 0.001^(Tc/RT);
N = fs*RT;
f = linspace(0,fs*(1-1/N),N);
t = linspace(0,length(x)/fs,length(x));
global q
q = 1;
%Number of rows of graphs
G1 = 3;
%Number of columbs of graphs
G2 = 1;
%Do this untill the user hits q
while q == 1
%Options presented to the user
input_label = input('Enter command (o, p_p, p_up, pro, s, RT, Tc, plot, quit): ','s');
switch input_label
case 'o'
%Open wav file
[x,fs] = wavread('Audio_1');
disp('**Audio File Opened**');
%If user wants to change reverberation time
case 'RT'
disp('Change RT to:');
RT_input = input('','s');
fprintf('**RT changed to:%s**\n',RT_input)
%If user wantes to change delay time
case 'Tc'
disp('Change Tc to:');
Tc_input= input('','s');
fprintf('**Tc changed to:%s**\n',Tc_input)
%PROBLEM%
%Can change RT and Tc in this script but not in the running program
%Doesnt seem to be able to access RT and Tc from the above RT and Tc 'cases'
case 'pro'
%print statments show that the values RT and Tc HAVE been changed when 'pro' is run
RT = RT_input;
%fprintf('**RT:%s**\n',RT)
Tc = Tc_input;
%fprintf('**Tc:%s**\n',Tc)
%Comb filter audio
b =[zeros(1, M) 1];
a =[1 zeros(1, M-1) -g];
H = (b/a); %Not necessary
%Filter function uses H(b/a) withing function
y = filter(b, a, x);
%Calculate impulse responce
[imp,f] = impz(b,a);
%Calculates the frequency responce using a & b
fr = freqz(b,a,N);
case 'plot'
%Plots the input wave
subplot(G1,G2,1);
plot(t,x);
title('Input');
%Plots the output wave
subplot(G1,G2,2);
plot(t,y,'r');
title('Output');
%Plots the impulse responce of output wave
subplot(G1,G2,3);
plot(f,imp);
title('Impulse Response');
case 'quit'
disp('**Program Terminated**')
q = 0;
return
otherwise
disp('Unrecognised input, please try again...')
end
end
以下是程序的运行方式:
用户:o
程序打开音频文件
用户:RT
然后用户输入RT值
用户:Tc
用户然后输入Tc的值
用户:专业
相应处理音频文件
用户:情节
绘制原始音频文件 绘制处理的音频文件 绘制频率响应
然后,用户应该可以更改RT和Tc的值,重新处理音频并使用与上面相同的步骤重新绘制。
值RT和Tc用于计算g,N和M(在代码顶部定义)。
当我在脚本运行时调用RT和Tc情况并输入新值时,就好像没有将新值分配给RT和Tc,这意味着图形不会按原样改变。
但是,如果在运行脚本之前更改RT和Tc的值,则会相应地更改图形。这意味着不会在不同情况下共享变量。
为什么?
%编辑:'pro'案例中的两个fprintf行导致matlab中断(无法重新运行程序) 如果我能找到原因,那就太棒了。
答案 0 :(得分:0)
将整个输入读取和分析过程放入while循环是一个可怕的想法,可以说,这是一个工作的最小版本的代码。
添加两个数字(RT
和Tc
)并绘制结果,按照您在OP中描述的那样工作:
用户:RT
然后用户输入RT值
用户:Tc
用户然后输入Tc的值
用户:专业
相应处理音频文件
用户:情节
q = 1;
%default
RT = 1;
Tc = 1;
%Do this untill the user hits q
while q == 1
%Options presented to the user
input_label = input('Enter command (RT, Tc, pro, plot, quit): ','s');
switch input_label
%If user wants to change reverberation time
case 'RT'
disp('Change RT to:');
RT_input = input('');
fprintf('**RT changed to:%s**\n',RT_input)
case 'Tc'
disp('Change Tc to:');
Tc_input= input('');
fprintf('**Tc changed to:%s**\n',Tc_input)
case 'pro'
RT = RT_input;
Tc = Tc_input;
res = RT + Tc;
case 'plot'
bar(res)
title('Amazing plot')
case 'quit'
disp('**Program Terminated**')
q = 0;
return
otherwise
disp('Unrecognised input, please try again...')
end
end