I am having a problem sharing variables across 'case' statements in Matlab. I am coding a comb filter which delays an audio file.
Here is the entire code:
%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
Here is how the program should be run:
User: o
Program opens audio file
User: RT
User then enters value for RT
User: Tc
User then enters value for Tc
User: pro
Audio file is processed accordingly
User: plot
Original audio file is plotted Processed audio file is plotted Frequency responce is plotted
User should then be able to change the values of RT and Tc, re-process the audio and re-plot using the same steps as above.
The values RT and Tc are used to calculate g, N and M (Which are defined at the top of the code).
When I call the RT and Tc cases when the script is running and input new values, it's as though the new values are not being assigned to RT and Tc which means the graphs don't change as they should.
However, if I change the values of RT and Tc before running the script, it changes the graphs appropriately. This means that the variables are not being shared across cases.
Why?
%EDIT: The two fprintf lines in the 'pro' case causes matlab to break(Can't re-run the program) Would be great if I could find out why this is too.