开发者

Splitting arrays in matlab

Greetings All

I'm trying to 1)split an array into multiple parts 2)export each part to separate wave files 3)re-import wav files and join them together to make sure the array data that was split up wasn't altered.

I can do all of these steps the problem is when I test for error I expect it should be something like 2.232e-15 which is almost no error however I get unexpected large numbers for error.

MAE = 0.046232 MXE = 0.14522 RMSE = 0.064035

How can I fix this so the error rate goes down? I thought the array was being split in sections and the cell data was being copied exactly but it's looking like that may not be the case, how can I fix this?

Code below:

%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); %use开发者_Go百科d 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  


I decided to use the reshape command and pad with zeros because it cuts the code down a lot and it should be quicker.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜