Code covered by the BSD License  

Highlights from
Spectral and phase magnitude

image thumbnail

Spectral and phase magnitude

by

 

30 Jan 2014 (Updated )

Illustrates the impact of Spectral Magnitude and Phase on Long-Time Analysis and Synthesis of Speech

[xin,xph,xmagn,xphr,timind]=phase_magnitude(xin,fs)
function [xin,xph,xmagn,xphr,timind]=phase_magnitude(xin,fs)
%
% from an original speech file, calculate the long-term phase and magnitude
% and play back speech waveform under each of three conditions, namely:
%   condition 1 -- original phase, unity magnitude
%   condition 2 -- original magnitude, zero phase
%   condition 3 -- original magnitude, random phase
%
% The results show that only the unity magnitude, original phase condition
% is intelligible, showing that preserving long-term phase is a lot more
% important than preserving long-term magnitude

% Inputs:
%   xin: original speech signal
%   fs: sampling rate of speech signal
%
% Outputs:
%   xin: original speech signal
%   xph: signal with magnitude set to 1.0 and with original phase
%   xmagn: signal with phase set to 0 and with original magnitude
%   xphr: signal with random phase and with original magnitude
%   timind: time index in seconds for displaying waveforms
    
% if number of samples is odd, append zero-valued sample for simple rotation
    if (mod(length(xin),2) == 1) xin(length(xin)+1)=0;
    end
    
% create time index (in seconds)
    timind=(0:length(xin)-1)/fs;
    
% long term spectral analysis
    XINZF=fft(xin,length(xin));
    
% condition 1 - set magnitude to 1 and resynthesize using original phase; 
% need to first create phase array using atan2 function; 
% then set magnitude to 1 and create complex
% transform from unity magnitude and real phase
    phase=atan2(imag(XINZF),real(XINZF));
    r=cos(phase);
    im=sin(phase);
    XINZFP=r+im*j;
    
% inverse transform back to time domain and set imaginary component of
% signal to zero (small residual from computational errors)
    xph=ifft(XINZFP,length(xin));
    xph=real(xph);
    
% scale to range of [-1 1]
    xphm=max(max(xph),-min(xph));
    xph=xph/xphm;
    
% condition 2 - set phase to 0 and resynthesize using original magnitude;
    phase=zeros(1,length(xin));
    mag=abs(XINZF);
    XINZFM=mag;
    
% inverse transform back to time domain
    xmag=ifft(XINZFM,length(xin));
    
% scale to range of [-1 1]
    xmagm=max(max(xmag),-min(xmag));
    xmag=xmag/xmagm;
    
% rotate signal to middle of range
    len=length(xmag);
    xmagn=[xmag(len/2+1:len); xmag(1:len/2)];
    
% condition 3 - set phase to random values (in range 0 to pi) and 
% resynthesize using original magnitude;
    phase=(rand(1,length(xin))-0.5)*2*pi;
    r=cos(phase).*XINZFM';
    im=sin(phase).*XINZFM';
    XINZFP=r+im*j;
    
% inverse transform back to time domain and set imaginary component of
% signal to zero (small residual from computational errors)
    xphr=ifft(XINZFP,length(xin));
    xphr=real(xphr);
    
% scale to range of [-1 1]
    xphrm=max(max(xphr),-min(xphr));
    xphr=xphr/xphrm;
end

Contact us