0
$\begingroup$

I am trying to modify an LTFAT demo script that records and plays back streaming audio simultaneously. The objective is to read in the block to a vector, and take the DFT of the block samples, and then save one phase value in the frequency bin per frame. Then, I'm trying to unwrap each individual phase and plot it in real time. The problem seems to occur when I pass the unwrapped phase value to my plot function, where the block seems to stop being processed altogether, and the plot displays a constant output of zero in time.

function demo_blockproc_basicloop(source,varargin) 

if demo_blockproc_header(mfilename,nargin)
   return;
end

figure
title('Unwrapped Phase Vs. Time');
xlabel('Seconds');
ylabel('Phase');

% Basic Control pannel (Java object)
p = blockpanel({
               {'GdB','Gain',-20,20,0,21},...
               });

% Setup blocktream
try
    fs = block(source,varargin{:},'loadind',p);
catch
    % Close the windows if initialization fails
    blockdone(p);
    err = lasterror;
    error(err.message);
end

% Set buffer length to 30 ms
L = floor(30e-3*fs);

flag = 1;
%Loop until end of the stream (flag)


while flag && p.flag

   gain = blockpanelget(p,'GdB');
   gain = 10^(gain/20);

   [f,flag] = blockread(L); 

   % The following does nothing in the rec only mode.
   blockplay(f*gain);

   xVec = zeros(1,length(f));
   XVec = zeros(1,1);
   N = length(f);

   % Calculate the DFT of the sample within the circular buffer for only
   % the first value of the frequency bin.

   for k = 1:1
       for n = 1:N-1           
           xVec(k) = f(n).*exp(-1j*2*pi*((-N/2)+n-1)*((-N/2)+k-1)/N);
       end
       XVec = sum(xVec);
   end

  % Take the unwrapped phase value of the first term of the DFT  

  P = unwrap(angle(XVec(1)));

  phaseplot(P);  

end
blockdone(p);

function phaseplot(P)

  % Creat plot sequence using P to display phase shift in scrolling time.  

  h = animatedline;
  ax = gca;
  ax.YGrid = 'on';

  % Change unwrapped phase limits to appropriate range.

  ax.YLim = [-50 100];
  startTime = datetime('now');

  stop = false;

  while ~stop              
      % Get current time
      t =  datetime('now') - startTime;
      % Add points to animation
      addpoints(h,datenum(t),P)
      % Update axes
      ax.XLim = datenum([t-seconds(15) t]);
      datetick('x','keeplimits')
      drawnow
  end   
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.