ita_demo_lambda_resonator.m 9.55 KB
Newer Older
1
function [ frames ] = ita_demo_lambdaResonator( varargin )
2
%ita_demo_LAMBDARESONATOR - +++ Short Description here +++
3 4 5 6 7 8
%  This function ++++ FILL IN INFO HERE +++
%
%  Syntax:
%   audioObjOut = ita_demo_lambdaResonator(options)
%
%   Options (default):
9
%           'frequency'         (100)    : the wave frequency
10 11
%           'numReflections'    (3)     : number of reflections
%           'R'                 (0.9)   : R
12 13
%           'resonatorType'     (2)     : 2 lambda/2 4 lambda/4
%           'maxXFactor'        (4)     : length of the plot
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
%           'plotSum'           (true)  : plot the sum of the waves
%           'plotWavelength'    (3)     : length of animation
%           'makeMovie'         (false) : return frames
%           'frameRate'         (10)    : framerate  
%
%  Example:
%   movieFrames = ita_demo_lambdaResonator(options)
%
%  See also:
%   ita_toolbox_gui, ita_read, ita_write, ita_generate
%
%   Reference page in Help browser 
%        <a href="matlab:doc ita_demo_lambdaResonator">doc ita_demo_lambdaResonator</a>

% <ITA-Toolbox>
% This file is part of the ITA-Toolbox. Some rights reserved. 
% You can find the license for this m-file in the license.txt file in the ITA-Toolbox folder. 
% </ITA-Toolbox>


% Author: Jan Gerrit Richter -- Email: jan.richter@rwth-aachen.de
% Created:  09-Apr-2015 

37
    sInit.frequency = 100;
38 39 40 41 42 43
    sInit.numReflections = 3;
    sInit.R = 0.9;
    sInit.makeMovie = 'false';
    sInit.frameRate = 10;
    sInit.plotSum = 'true';
    sInit.plotWavelength = 3;
44 45 46
    sInit.resonatorType = 4;
    sInit.maxXFactor = 4;
    sArgs        = struct('frequency',sInit.frequency,'numReflections',sInit.numReflections,'R',sInit.R,'makeMovie',sInit.makeMovie,'frameRate',sInit.frameRate,'plotSum',sInit.plotSum,'plotWavelength',sInit.plotWavelength,'resonatorType',sInit.resonatorType,'maxXFactor',sInit.maxXFactor);
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    [sArgs] = ita_parse_arguments(sArgs,varargin);

    p_hat = 1;
    c= 344;


    f = sArgs.frequency;

    omega = 2*pi*f;

    k = omega/c;

    lambda = c/f;


    lambda100 = c/100;

    % x axis from 0 to 2 lambda of 100 Hz
65 66 67 68 69 70 71 72 73
    if sArgs.resonatorType == 4
        maxX = lambda100/4;
        maxX = maxX*sArgs.maxXFactor;
    else
        maxX = lambda100/2; 
        maxX = maxX*sArgs.maxXFactor;
    end
    
    x = 0:0.01:maxX;
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

    % the time axis from 0 in resonable steps up to set wavelength numbers
    maxTime = sArgs.plotWavelength*lambda/c;
    deltaT = 0.003/sArgs.frameRate;
    t = 0:deltaT:maxTime;

    % reflection cooef of the walls
    R = sArgs.R;

    optionsStruct = createOptions(p_hat,c,f,omega,k,x,t,R,lambda,lambda100,sArgs);

    % the time part of all the equations
    timeTerm = exp(1i*omega.*t);
    timeTerm = repmat(timeTerm,length(x),1).';

    % this is done. amplitude times space configuration and time movement
%     pressure = p_hat*(spaceTerm).*(timeTerm);
    
    % add the original wave
93 94 95 96 97 98 99
    optionsStruct.R = 1;
    data.pressure{1} = addWave(optionsStruct,timeTerm,1,0);
    
    R = sArgs.R;

    optionsStruct = createOptions(p_hat,c,f,omega,k,x,t,R,lambda,lambda100,sArgs);
    
100 101
    % add all the reflection waves
    for index = 1:sArgs.numReflections
102 103 104 105 106 107 108 109 110 111
        if sArgs.resonatorType == 4
            if mod(index,3) ~= 0
                phaseShift = 180;
            else
                phaseShift = 0;
            end
        else
           phaseShift = 0; 
        end
        data.pressure{end+1} = addWave(optionsStruct,timeTerm,index+1,phaseShift);
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    end
      
    % if we have reflections and want to see the sum, calculate it
    if sArgs.numReflections > 0 && sArgs.plotSum 
        pressureAdd = [];
        for index = 1:length(data.pressure)
            if (isempty(pressureAdd))
                pressureAdd = data.pressure{index};
            else
                pressureAdd =  pressureAdd + data.pressure{index};
            end
        end

        data.pressure{end+1} = pressureAdd;
    end
    
    
    % make the plot
    
    data.index = [];
    handles = [];
    frames = {};
    data.index = 1;
    handles = makePlot(data,handles,optionsStruct,1);
    for index = 1:length(t)
        data.index = index;
        handles = makePlot(data,handles,optionsStruct,0);
        % if we want to create a movie, get the fames and return them
        if (sArgs.makeMovie == true)
            frames{index} = getframe(gcf);
        end
%         pause(0.1);
        drawnow;
    end

end

function handles = makePlot(data,handles,oS,plotLegend)

    % the first time, the plot is called, create a fullscreen figure
    % fullscreen is important to video quality
    if plotLegend
        figure('units','normalized','outerposition',[0 0 1 1]);
    end

    sArgs = oS.sArgs;
    x = oS.x;
    lambda = oS.lambda100;

    pressure = data.pressure;
    timeIndex = data.index;

    % if the plots exist, just change the data, if not, plot them
    newHandles = ~isempty(handles);
    for index = 1:length(data.pressure)
        pressure = data.pressure{index};
        
        if (newHandles)
            h = handles(index);
            set(h,'XData',x/lambda,'YData',real(pressure(timeIndex,:)));
        else
            if (index == length(data.pressure)) && sArgs.plotSum % plot thicker if add
                handles(index) = plot(x/lambda,real(pressure(timeIndex,:)),'LineWidth',5,'Color',[0.466 0.674 0.188]);
            else
                handles(index) = plot(x/lambda,real(pressure(timeIndex,:)),'LineWidth',3);
            end
        end
        hold all
        grid on
    end
    
    % set some limits
    ylim([-3.5*oS.p_hat 3.5*oS.p_hat])
185
    xlim([-0.1 max(x)/lambda+0.1])
186 187 188 189 190 191 192 193 194 195 196
    
    % some legend as well
    switch(oS.sArgs.numReflections)
        case 0
            legend({'Original Wave'})
        case 1
            if (sArgs.plotSum)
                legend({'Original Wave','1. Reflection','Sum'})
            else
                legend({'Original Wave','1. Reflection'})
            end
197 198 199 200 201 202
        case 2
            if (sArgs.plotSum)
                legend({'Original Wave','1. Reflection','2. Reflection','Sum'})
            else
                legend({'Original Wave','1. Reflection','2. Reflection'})
            end
203 204 205 206 207 208 209 210
        otherwise
            if (sArgs.plotSum)
                legend({'Original Wave','1. Reflection','2. Reflection','3. Reflection','Sum'})
            else
                legend({'Original Wave','1. Reflection','2. Reflection','3. Reflection'})
            end
    end

211 212 213 214 215 216 217 218 219 220 221
    if length(handles) <= length(data.pressure)
        if sArgs.resonatorType == 2
            % create the vertical boundaries
            switch(oS.sArgs.numReflections)
                case 0
                    xg = [];
                case 1
                    xg = [max(x/lambda)];
                otherwise
                    xg = [0 max(x/lambda)];      
            end
222

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
            ylim([-3.5*oS.p_hat 3.5*oS.p_hat])
            xlim([-0.1 2.1])

            yg = get(gca,'YLim');
            xx = reshape([xg;xg;NaN(1,length(xg))],1,length(xg)*3);
            yy = repmat([yg NaN],1,length(xg));
            handles(index+1) = plot(xx,yy,'k','LineWidth',3);
        else
            ylim([-3.5*oS.p_hat 3.5*oS.p_hat])
            xlim([-0.1 2.1])

            % create the vertical boundaries
            switch(oS.sArgs.numReflections)
                case 0
                    xg = [];
                case 1
                    xg = [max(x/lambda)];
                    
                    yg = get(gca,'YLim');
                    xx = reshape([xg;xg;NaN(1,length(xg))],1,length(xg)*3);
                    yy = repmat([yg NaN],1,length(xg));
                    handles(index+1) = plot(xx,yy,'k--','LineWidth',3); 
                otherwise
                    xg = [0];
                    yg = get(gca,'YLim');
                    xx = reshape([xg;xg;NaN(1,length(xg))],1,length(xg)*3);
                    yy = repmat([yg NaN],1,length(xg));
                    handles(index+1) = plot(xx,yy,'k','LineWidth',3);  
                    
                    xg = [max(x/lambda)];
                    yg = get(gca,'YLim');
                    xx = reshape([xg;xg;NaN(1,length(xg))],1,length(xg)*3);
                    yy = repmat([yg NaN],1,length(xg));
                    handles(index+1) = plot(xx,yy,'k--','LineWidth',3); 
            end
        end

%     hold all
    end
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    % change the font size and put in some titles
    set(gca,'FontSize',20,'fontWeight','bold')
    title(sprintf('%d Hz',oS.f));
    xlabel('\lambda of 100 Hz')
    

end

function optionsStruct = createOptions(p_hat,c,f,omega,k,x,t,R,lambda,lambda100,sArgs)
    optionsStruct.p_hat = p_hat;
    optionsStruct.c = c;
    optionsStruct.f = f;
    optionsStruct.omega = omega;
    optionsStruct.k = k;
    optionsStruct.x = x;
    optionsStruct.t = t;
    optionsStruct.R = R;
    optionsStruct.lambda = lambda;
    optionsStruct.lambda100 = lambda100;
    optionsStruct.sArgs = sArgs;

end

285
function pressureRef = addWave(oS,timeTerm,number,rPhase)
286 287 288 289 290 291 292
    
    % depending on the reflection number, the wave has to move forward or
    % backward
    expTerm = exp((-1)^number*1i*oS.k*oS.x);
    reflectionTerm = repmat(expTerm,length(oS.t),1);    
    
    % add the reflection factor and amplitude 
293
    wave = (oS.R)^(number-1)*oS.p_hat.*reflectionTerm*exp(1i*deg2rad(rPhase));
294 295 296 297 298 299 300 301 302 303 304 305 306 307
    
    % the starting phase (at x = 0) is dependend on the number of
    % reflections (the distance the wave has traveled so far)
    if number > 1
        if mod(number,2) == 0
            wave = wave.*exp(-1i*oS.k*(number)*max(oS.x));
        else
            wave = wave.*exp(-1i*oS.k*(number-1)*max(oS.x));
        end
    end
    
    % multiply the space movement with the time movement
    pressureRef = wave.*timeTerm;
end