ita_demo_lambda_resonator.m 7.12 KB
Newer Older
1
%% Lambda Resonator Demo Function (see input arguments)
2
function [ frames ] = ita_demo_lambdaResonator( varargin )
3

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
%  This function ++++ FILL IN INFO HERE +++
%
%  Syntax:
%   audioObjOut = ita_demo_lambdaResonator(options)
%
%   Options (default):
%           'frequency'         (88)    : the wave frequency
%           'numReflections'    (3)     : number of reflections
%           'R'                 (0.9)   : R
%           '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 

    sInit.frequency = 88;
    sInit.numReflections = 3;
    sInit.R = 0.9;
    sInit.makeMovie = 'false';
    sInit.frameRate = 10;
    sInit.plotSum = 'true';
    sInit.plotWavelength = 3;
    sArgs        = struct('frequency',sInit.frequency,'numReflections',sInit.numReflections,'R',sInit.R,'makeMovie',sInit.makeMovie,'frameRate',sInit.frameRate,'plotSum',sInit.plotSum,'plotWavelength',sInit.plotWavelength);
    [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
    x = 0:0.01:2*lambda100;

    % 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
    data.pressure{1} = addWave(optionsStruct,timeTerm,1);
    % add all the reflection waves
    for index = 1:sArgs.numReflections
        data.pressure{end+1} = addWave(optionsStruct,timeTerm,index+1);
    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])
    xlim([-0.1 2.1])
    
    % 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
        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

    % create the vertical boundaries
    switch(oS.sArgs.numReflections)
        case 0
            xg = [];
        case 1
            xg = [max(x/lambda)];
        otherwise
            xg = [0 max(x/lambda)];      
    end

    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));
    h_minorgrid = plot(xx,yy,'k','LineWidth',3);
    
    hold all
    
    % 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

function pressureRef = addWave(oS,timeTerm,number)
    
    % 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 
    wave = (oS.R)^number*oS.p_hat.*reflectionTerm;
    
    % 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