2014-11-12 2 views
-3

Matlab의 'case'문장에서 변수를 공유하는 데 문제가 있습니다. 오디오 파일을 지연시키는 콤 필터를 코딩 중입니다.Matlab 스위치 상태 변수 공유

사용자 :

프로그램이 열립니다 O를 오디오 파일

사용자 : RT

다음
%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 

프로그램이 실행해야하는 방법이다 : 여기

은 전체 코드입니다

사용자가 RT

에 대한 값을 입력합니다.

사용자 : TC는

사용 후 TC에 대한

사용자 값을 입력 : 프로

오디오 파일에 따라 처리됩니다

는 사용자 : 플롯 원본 오디오 파일 을 꾸몄다

은 가공 오디오 파일이 플롯됩니다 주파수 응답이 플롯됩니다

사용자는 RT와 Tc의 값을 변경하고 위와 동일한 단계를 사용하여 오디오를 다시 처리하고 다시 플로팅 할 수 있어야합니다.

값 RT와 Tc는 g, N 및 M (코드 상단에서 정의 됨)을 계산하는 데 사용됩니다.

스크립트가 실행 중일 때 RT 및 Tc 케이스를 호출하고 새 값을 입력하면 RT 및 Tc에 새 값이 할당되지 않은 것입니다. 즉 그래프가 변경되지 않는다는 의미입니다.

그러나 스크립트를 실행하기 전에 RT 및 Tc 값을 변경하면 그래프가 적절하게 변경됩니다. 이는 변수가 여러 사례에서 공유되지 않는다는 것을 의미합니다.

이유에 대한 제안이 있으십니까?

감사

%의 편집 : '프로'경우 라인 fprintf와 두 MATLAB이 중단됩니다 (캔트 다시 실행 프로그램)이 너무 왜 찾을 수 있다면 이 좋을 것입니다.

+2

당신이 문제를 재현하기에 충분한 코드를 공유 할 수 있을까요? 'RT_input'을 출력 변수로 전달하고 다시 호출 할 때 함수에 넘겨 줍니까? – Arpi

+0

나는 내 질문을 크게 편집했다. 잘하면이 일이 더 명확해질 것이다. –

+0

왜 입력을 문자열로 사용합니까? 'RT_input = input ('', 's'); ' – Arpi

답변

0

전체 입력 읽기 및 분석 프로세스를 while 루프에 넣는 것은 끔찍한 생각입니다. 즉, 여기서 말하는 최소한의 코드가 작동합니다.

사용자 : RT

사용 후 RT

사용자에 대한 값을 입력 : TC

하는 두 숫자 (RTTc)를 추가하고 결과를 플롯, 당신은 OP에 설명 된대로 작동

사용자가 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