Added new process function for itaResult

-interpolation in frequency domain
-conversion to itaAudio in frequency domain
parent 7e16d1a9
function audioObj = ita_result2audio_spk(resultObj, samplingRate, fftDegree, varargin)
%ita_result2audio_spk - Converts an itaResult with frequency data into
%itaAudio using interpolation and extrapolation in the frequency domain
% Data outside of the valid frequency range is extrapolated with zeros.
% Optionally, a filter can be applied.
%
% Syntax: itaResult = ita_result2audio_spk(itaResult, samplingRate, fftDegree, Options)
%
% Options (default):
% filter ('none'): 'none', 'lowpass', 'highpass', 'bandpass'
% filterorder (20): order for the filtering
%
% See also ita_result2audio, ita_interpolate_spk_result
%
% Reference page in Help browser
% <a href="matlab:doc ita_result2audio_spk">doc ita_result2audio_spk</a>
%
% Autor: Philipp Schfer -- Email: psc@akustik.rwth-aachen.de
% <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>
%% For error handling
thisFuncStr = [upper(mfilename) ':'];
%% Initialization and Input Parsing
sArgs = struct('pos1_data','itaResult','pos2_samplingRate','integer', 'pos3_fftDegree','integer');
ita_parse_arguments(sArgs,{resultObj, samplingRate, fftDegree});
assert(logical(resultObj.isFreq), [thisFuncStr 'Data must be in frequency domain.'])
sArgs = struct('filter', 'none', 'filterorder', 20);
sArgs = ita_parse_arguments(sArgs,varargin);
doLowPass = isequal(sArgs.filter, 'bandpass') || isequal(sArgs.filter, 'lowpass');
doHighPass = isequal(sArgs.filter, 'bandpass') || isequal(sArgs.filter, 'highpass');
doBandPass = isequal(sArgs.filter, 'bandpass');
[~, nBins] = ita_get_nSamples(fftDegree);
fMin = resultObj.freqVector(1);
fMax = resultObj.freqVector(end);
%% Interpolation
newFreqs = linspace(0,samplingRate/2,nBins);
resultObj = ita_interpolate_spk_result(resultObj, newFreqs, 'absphase', true, 'extrap', 'zeros');
%% Conversion
sObj = saveobj(resultObj);
sObj = rmfield(sObj,[{'classname','classrevision'},itaResult.propertiesSaved]);
audioObj = itaAudio(sObj);
audioObj.signalType = 'energy';
audioObj.samplingRate = samplingRate;
%% Filter
upperExtrapDone = fMax < audioObj.freqVector(end);
lowerExtrapDone = fMin > audioObj.freqVector(1);
if doBandPass && upperExtrapDone && lowerExtrapDone
audioObj = ita_mpb_filter(audioObj, [fMin, fMax], 'order', sArgs.filterorder);
elseif doHighPass && upperExtrapDone
audioObj = ita_mpb_filter(audioObj, [0, fMax], 'order', sArgs.filterorder);
elseif doLowPass && lowerExtrapDone
audioObj = ita_mpb_filter(audioObj, [fMin, 0], 'order', sArgs.filterorder);
end
\ No newline at end of file
function [ varargout ] = ita_interpolate_spk_result(varargin)
%ITA_INTERPOLATE_SPK_RESULT - Rezising of frequency data through interpolation in frequency domain
% This Function interpolates an itaResult object in frequency domain
%
% Syntax: itaResult = ita_interpolate_spk_itaResult(itaResult, newFreqBins, Options)
%
% Options (default):
% method ('spline'): method used for interpolation, see help interp1 for more infos
% absphase (false) : interpolate complex data via magnitude/phase or real/imaginary data
% extraplow ('zeros'): method used for extrapolation to lower frequencies
% extraphigh ('zeros'): method used for extrapolation to higher frequencies
% extrap: use this to set both extrapolation methods at once
%
% Valid for extrapolation methods: ['zeros', 'const', 'interpmethod']
% - zeros: using zeros for constant extrapolation
% - const: using value of upper/lower frequency limit for constant extrapolation
% - interpmethod: using same method as for interpolation
%
% See also ita_interpolate_spk, ita_mpb_filter, ita_multiply_spk, ita_audioplay.
%
% Reference page in Help browser
% <a href="matlab:doc ita_interpolate_spk_result">doc ita_interpolate_spk_result</a>
%
% Autor: Philipp Schfer -- Email: psc@akustik.rwth-aachen.de
% <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>
%% For error handling
thisFuncStr = [upper(mfilename) ':'];
%% Initialization and Input Parsing
sArgs = struct('pos1_a','itaResult','pos2_newFreqs','numeric','method','spline','absphase',false, 'extraplow', 'zeros', 'extraphigh', 'zeros', 'extrap', '');
[data,newFreqs, sArgs] = ita_parse_arguments(sArgs,varargin);
oldFreqs = data.freqVector;
if isrow(newFreqs); newFreqs = newFreqs'; end
if ~isempty(sArgs.extrap)
sArgs.extraplow = sArgs.extrap;
sArgs.extraphigh = sArgs.extrap;
end
switch sArgs.extraplow
case 'zeros'
extrapLow = zeros(1, size(data.freqData, 2));
case 'const'
extrapLow = data.freqData(1, :);
case 'interpmethod'
extrapLow = [];
otherwise
error([thisFuncStr 'Invalid value for extraplow option.'])
end
switch sArgs.extraphigh
case 'zeros'
extrapHigh = zeros(1, size(data.freqData, 2));
case 'const'
extrapHigh = data.freqData(end, :);
case 'interpmethod'
extrapHigh = [];
otherwise
error([thisFuncStr 'Invalid value for extraphigh option.'])
end
%% Interpolation
if sArgs.absphase %Interpolate abs/phase (gdelay would be even better)
newFreqData = interp1(oldFreqs,abs(data.freqData),newFreqs,sArgs.method, 'extrap') .* ...
exp(1i * interp1(oldFreqs,unwrap(angle(data.freqData)),newFreqs,sArgs.method, 'extrap'));
else %Interpolate real/img
newFreqData = interp1(oldFreqs,data.freqData,newFreqs,sArgs.method, 'extrap');
end
%% Extrapolation
if ~isempty(extrapLow)
idxOverwriteLow = ceil(newFreqs) < data.freqVector(1);
if any(idxOverwriteLow)
newFreqData(idxOverwriteLow, :) = repmat(extrapLow, sum(idxOverwriteLow), 1);
end
end
if ~isempty(extrapHigh)
idxOverwriteHigh = floor(newFreqs) > data.freqVector(end);
if any(idxOverwriteHigh)
newFreqData(idxOverwriteHigh, :) = repmat(extrapHigh, sum(idxOverwriteHigh), 1);
end
end
%% Finishing
data.freqVector = newFreqs;
data.freqData = newFreqData;
%% Add history line
data = ita_metainfo_add_historyline(data,mfilename,varargin);
%% Find Output
varargout(1) = {data};
end %function
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment