-3

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Lewis Thresh
  • 41
  • 1
  • 7
  • 2
    Could you share enough code to reproduce the problem? Do you pass `RT_input` as an output variable and pass it to the function (or whatever you posted) when calling again? – rozsasarpi Nov 12 '14 at 15:06
  • I have drastically edited my question, hopefully this will make things clearer – Lewis Thresh Nov 12 '14 at 16:11
  • Why do you take the input as string?, e.g. `RT_input = input('','s');` – rozsasarpi Nov 12 '14 at 16:37
  • This is a good point, but according to matlab it must be an 's'. How do I then change this into a double? – Lewis Thresh Nov 12 '14 at 16:46
  • `str2num`, I wouldn't be sure that `'s'` is necessary; [first example](http://www.mathworks.com/help/matlab/ref/input.html). – rozsasarpi Nov 12 '14 at 16:48
  • The code you provided is a mess, e.g. `M = Tc/Ts;` where `Tc` is provided by the user and `M` is used: `b =[zeros(1, M) 1];` without ensuring that `M` is integer. Until you do not post a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) you will hardly get any help. – rozsasarpi Nov 12 '14 at 16:56
  • I am brand new to matlab and am working on this as a Uni project. This code is being used for a specific purpose and all variables are picked specifically for this code to work. I am here to learn things and correct mistakes, not post professional code. – Lewis Thresh Nov 13 '14 at 17:03
  • Sorry if you feel offended, I am just suggesting you to try to reduce your code and make it runnable, [guidelines](http://stackoverflow.com/help/mcve). I wanted to help you but I wasn't able to run your code without error, and could not reproduce the problem you described. Everyone is a volunteer here and has plenty of other problems to work on. – rozsasarpi Nov 13 '14 at 17:35

1 Answers1

0

It is a horrible idea to put the whole input reading and analysis process into a while loop, that being said, here is a working minimal version of your code.

Add two numbers (RT and Tc) and plot the result, works as you described in the OP:

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

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
rozsasarpi
  • 1,621
  • 20
  • 34