我有一個運行同一函數的兩個副本的MATLAB腳本,我想在兩個不同的內核上並行運行它們。用於在兩個不同內核上運行兩個獨立函數的MATLAB Parallel Toolbox
這功能我打電話
function [outputVector] = ParallelPitchShift(inputVector, windowSize, hopSize, step)
%% Parameters
% Window size
winSize = windowSize;
% Space between windows
hop = hopSize;
% Pitch scaling factor
alpha = 2^(step/12);
% Intermediate constants
hopOut = round(alpha*hop);
% Hanning window for overlap-add
wn = hann(winSize*2+1);
wn = wn(2:2:end);
%% Source file
x = inputVector;
% Rotate if needed
if size(x,1) < size(x,2)
x = transpose(x);
end
x = [zeros(hop*3,1) ; x];
%% Initialization
% Create a frame matrix for the current input
[y,numberFramesInput] = createFrames(x,hop,winSize);
% Create a frame matrix to receive processed frames
numberFramesOutput = numberFramesInput;
outputy = zeros(numberFramesOutput,winSize);
% Initialize cumulative phase
phaseCumulative = 0;
% Initialize previous frame phase
previousPhase = 0;
for index=1:numberFramesInput
%% Analysis
% Get current frame to be processed
currentFrame = y(index,:);
% Window the frame
currentFrameWindowed = currentFrame .* wn'/sqrt(((winSize/hop)/2));
% Get the FFT
currentFrameWindowedFFT = fft(currentFrameWindowed);
% Get the magnitude
magFrame = abs((currentFrameWindowedFFT));
% Get the angle
phaseFrame = angle(currentFrameWindowedFFT);
%% Processing
% Get the phase difference
deltaPhi = phaseFrame - previousPhase;
previousPhase = phaseFrame;
% Remove the expected phase difference
deltaPhiPrime = deltaPhi - hop * 2*pi*(0:(winSize-1))/winSize;
% Map to -pi/pi range
deltaPhiPrimeMod = mod(deltaPhiPrime+pi, 2*pi) - pi;
% Get the true frequency
trueFreq = 2*pi*(0:(winSize-1))/winSize + deltaPhiPrimeMod/hop;
% Get the final phase
phaseCumulative = phaseCumulative + hopOut * trueFreq;
% Remove the 60 Hz noise. This is not done for now but could be
% achieved by setting some bins to zero.
%% Synthesis
% Get the magnitude
outputMag = magFrame;
% Produce output frame
outputFrame = real(ifft(outputMag .* exp(j*phaseCumulative)));
% Save frame that has been processed
outputy(index,:) = outputFrame .* wn'/sqrt(((winSize/hopOut)/2));
end
%% Finalize
% Overlap add in a vector
outputTimeStretched = fusionFrames(outputy,hopOut);
% Resample with linearinterpolation
outputTime = interp1((0:(length(outputTimeStretched)-1)),outputTimeStretched,(0:alpha:(length(outputTimeStretched)-1)),'linear');
% Return the result
outputVector = outputTime;
return
這是我在做什麼來調用函數
clear
x = wavread('x2.wav');
y1 = ParallelPitchShift(x,1024,256,7);
y2 = ParallelPitchShift(x,1024,256,12);
output = x(1:417000)' + y1(1:417000) + y2(1:417000);
sound(x,44100)
pause
sound(output,44100)
是否有可能做到這一點?請讓我知道,謝謝!
我試過了,但功能有一個用於內部的循環不能並行... MATLAB給了我一個錯誤。 – user123668 2011-04-14 03:52:27
是否可以在不使用parfor的情況下完成? – user123668 2011-04-14 03:52:59
你有沒有試過在另一個函數中包裝你的代碼的麻煩部分? – abcd 2011-04-14 04:22:06