在matlab中拆分数组

时间:2011-04-19 07:03:45

标签: arrays matlab octave

问候全部

我正在努力 1)将数组拆分成多个部分 2)将每个部分导出为单独的波形文件 3)重新导入wav文件并将它们连接在一起以确保 拆分的数组数据没有改变。

我可以在测试时完成所有这些步骤 错误我希望它应该像2.232e-15那样 几乎没有错误,但我得到了意想不到的大数字 错误。
MAE = 0.046232 MXE = 0.14522 RMSE = 0.064035

如何解决此问题,以便错误率降低? 我认为数组被分割成部分并且正在复制单元格数据 确切但看起来可能不是这样, 我该如何解决这个问题?

以下代码:

%split_file
%create sine wave signal
clear all, clc
tic
fs = 44100;                   % Sampling frequency
t=linspace(0,1,fs);
freq=340;
ya = sin(2*pi*freq*t); %+ 1*sin(2*pi*250*t); 
[size_r,size_c]=size(ya');

jj=[];
kk=0;
wavefilesplit=[];

%need to delete diretory and recreate it to clean out files
fileprepathStr='/home/rat/Documents/octave/pre/'; %
rmdir(fileprepathStr,'s');
fprintf('\n-1- deleting %s directory %2.4f sec',fileprepathStr,toc);
mkdir(fileprepathStr);
fprintf('\n-1- creating %s directory %2.4f sec',fileprepathStr,toc);


jj=1;
for ii=1:fs/4:size_r,  %build array of desired ranges or fs/2
    jj(end+1,:)=ii-1;  %minus 1 to get correct array index in cell
end;

[size_rjj,size_cjj]=size(jj); %used to get size of jj array
jj(end+1,:)=size_r-(size_rjj-2); %adds the end of the sound file to the end of the jj array minus the amount of files joined

jj(2,:)=[]; %deletes second cell with zero and shifts the cells up

for ii=1:1:size_rjj-1,kk=kk+1;

    wavefilesplit=ya(jj(kk):jj(kk+1));
    wavefn=strcat('wavefn_',num2str(kk,'%04d')); %build filename dynamiclly with 4 leading zeros
    wavwrite([wavefilesplit],fs,16,strcat('/home/rat/Documents/octave/pre/',wavefn,'.wav')); 
    fprintf('\n-1- wavwrite split %s.wav %3.0f of %3.0f %6.3fsec  %6.3fmins\n',wavefn,kk,size_rjj-1,toc,toc/60);
end; 
fprintf('\n-2- Elapsed time in seconds after wavwrite split %6.3fsec  %6.3fmins\n',toc,toc/60);

%rejoin to check if arrays are the same
y2=[]; %
yb2=[];
filepathprocStr='/home/rat/Documents/octave/pre/';
files2=strcat(filepathprocStr,'*.wav');
files2=dir(files2);
[rwsz_files2,clsz_files2]=size(files2); %used to get ro and col size
for i=1:numel(files2) 
    [yb2, fs2, nbits] = wavread(strcat(filepathprocStr,files2(i).name));
    yb2=yb2';
    y2=[y2;yb2];    %Append files2
    fprintf('\n %4.0f of %4.0f joined %s',i,rwsz_files2,files2(i).name) 
end;
wavwrite([y2],fs2,16,'/home/rat/Documents/octave/pre/All_joined.2wav')

fprintf(' \n Done!!!\n');
ya=ya';
dy   = abs(ya-y2);           % absolute error
MAE  = mean(dy)               % 7.2292e-015   mean-absolute-error
MXE  = max(dy)                 % 3.4195e-014   maximum-absolute-error
RMSE = sqrt(mean(dy.^2))  % 9.5049e-015   root-mean-sqare-error  

1 个答案:

答案 0 :(得分:0)

我决定使用reshape命令并使用零填充,因为它会大大减少代码并且应该更快。

clear all, clc
ya=1:64;
fs=9;

padlen = mod(-length(ya), fs); %creates number of zeros needed to get correct array reshaped
ya_reshaped = reshape([ya zeros(1,padlen)], fs, []); % used to pad zeros on 1 col x rows
[size_r,size_c]=size(ya_reshaped)

wavefilesplit=[];
wavefilesplit2=[];
for ii=1:size_c
    wavefilesplit=ya_reshaped(:,ii) %this line can be used to export data/audio to file
    %could use if else statment to strip zeros off end if inserted here 
    wavefilesplit2=[wavefilesplit2; ya_reshaped(:,ii)] %will append to end all in one col to error check
end;
wavefilesplit2(end-(padlen-1):end)=[] %will erase zeros at the end of array wavefilesplit2(end-(padlen-1):end,1)=2 will ad 2's