在matlab中分割数组

发布于 2024-11-02 08:24:34 字数 2572 浏览 1 评论 0原文

问候

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

我可以执行所有这些步骤,问题是当我测试时 错误我希望它应该类似于 2.232e-15 几乎没有错误,但是我得到了意想不到的大量数字 错误。
平均误差=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  

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); %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  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄凉少年不暖心 2024-11-09 08:24:34

我决定使用 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文