2016-10-09 22 views
0

私は以前に私の実際の問題に関連する質問をしました。プログラムの大胆さを使用して、MP3ファイルをwavファイルに変換しました。 wavファイルのTh持続時間は約10秒です。私は10の部分に分割したい、つまり、各部分は1秒かかる。どうしたらいいの?コミュニティが私に何らかの答えを提供したとしても、私の無知を謝罪してください。Matlabでwavファイルを分割する

[y,fs]=wavread('UnchainMyHeart'); 
t=linspace(0,length(y)/fs,length(y)); 
plot(t,y) 

ここのコードは、自分のwavファイルを時間ドメインで表示していますが、wavファイルがないため表示されません。関係ない。さて、私の質問に合わせてそこから続けるのは可能でしょうか?私はそれが行列と何かがあると信じています... 私は知識が不足しているので、私はいつもあなたの助けに感謝します!

答えて

0

これは、次に何をしたいかによって異なります。信号の一部を取得する最も簡単な方法は次のとおりです。

part=y(1:Fs); 

あなたはprobalyこの物事の配列がそうで使用したい、またはこのようなsructure、なめらかWHILE:

%in loop 
part(i)=(i*Fs:i*Fs+Fs); 
i=i+1; 

は、配列の長さ

を意識します
0

同じ長さのチャンクでwavを読み込むクラスはWavReader.mです。

、1秒のチャンクを読んで、このようにそれを使用する:ここで

wr = WavReader('c:/wavs/killyourmama.wav'); 
wr.SetChunkLengthInMiliSeconds(1000.0);  
while(true)     
    [pcm, num_read] = wr.ReadChunk(); 

    if(num_read==0); break; end; 

    fprintf('Progress: %5.2f%%\n', wr.Progress()); 
end 

がクラスである:

classdef WavReader < handle 
    %WavReader Reads wav files in chunks 
    % Flexible wav chunk reader. 
    % 
    % 1) Usage steps in case of full file read: 
    %  
    %  Create wav reader: 
    %   wr = WavReader(path); 
    %  Read all samples: 
    %   [samples, count] = GetAllSamples(); 
    % 
    % 2) Usage steps in case of range read: 
    % 
    %  Create wav reader: 
    %   wr = WavReader(path); 
    % 
    %  Read samples from within a range:  
    %   [samples, count] = GetSamples(seconds_start, seconds_finish); 
    % 
    % 3) Usage steps in case chunk-by-chunk read: 
    % 
    %  Create wav reader: 
    %   wr = WavReader(path); 
    % 
    %  Set range of interest: 
    %   wr.SetRange(seconds_start, seconds_finish); 
    % 
    %  Set chunk size you want to read: 
    %   wr.SetChunkLength(chunk_size); 
    % 
    %  Read chunk-by-chunk: 
    %   while(num_read)     
    %    [pcm, num_read] = wr.ReadChunk(); 
    % 
    % 

    properties (Access=private) 
     % Info 
     path; 
     fs; 
     total_samples; 
     total_seconds; 

     % Range of interest   
     range_length; 
     range_samples; 

     % Current state of chunks 
     chunk_length; 
     total_chunks; 
     chunks_read; 
    end 

    methods 
     function obj = WavReader(in_path_wav) 
      % Constructor 
      try 
       obj.path   = in_path_wav; 

       info    = audioinfo(obj.path); 
       obj.fs    = info.SampleRate;  
       obj.total_samples = info.TotalSamples; 
       obj.total_seconds = info.Duration;     
      catch 
       error('Problem with opening wav file. Exiting.')  
      end 

      % Defaul reads full range, whole file. 
      obj.SetRangeFull(); 
     end 
     %% 
     function [range_length] = SetRangeFull(obj) 
      %SetRangeFull Sets the range to full (we are interested in all samples). 
      try 
       obj.range_samples = audioread(obj.path); 
      catch 
       error('Problem with reading wav file. Exiting.')      
      end 

      obj.range_length = obj.total_samples; 
      range_length = obj.range_length; 
     end 
     %% 
     function [range_length] = SetRange(obj, start_sec, end_sec) 
      % SetRange Sets target range of interest. 
      % 
      %# [range_length] = SetRange(obj, start_sec, end_sec) 
      %# INPUT start_sec: staring point in target range 
      %# INPUT end_sec: ending point in target range 
      %# OUTPUT range_length: number of total samples available in the range 
      % 
      % To use full range call SetRangeFull(). 
      % If start beyond end: throws exception. 
      % If end beyond end: sets end to end. 

      [start_sec, end_sec] = obj.checks(start_sec,end_sec); 

      % Full range if both zeroes: 
      %if(start_sec==0 && end_sec==0) 
      % start_sample = 1; 
      % stop_sample = obj.total_samples; 
      %else 
      start_sample = obj.seconds_to_samples(start_sec); 
      stop_sample = obj.seconds_to_samples(end_sec); 
      %end 

      obj.range_length = stop_sample - start_sample; 

      obj.range_samples = 0; 
      try 
       obj.range_samples = audioread(obj.path, [start_sample, stop_sample]); 
      catch 
       error('Problem with reading wav file. Exiting.')      
      end 

      range_length = obj.range_length; 
     end 
     %% 
     function [total_chunks] = SetChunkLength(obj, chunk_length) 
      % SetChunkLength Defines chunk length for chunk-by-chunk read. 
      % 
      %# [total_chunks] = SetChunkLength(obj, chunk_length) 
      %# INPUT chunk_length: desired chunk length 
      %# OUTPUT total_chunks: total number of available full chunks 
      % 
      % Abandonds last incomplete chunk. 

      obj.chunk_length = chunk_length; 
      obj.chunks_read = 0; 

      obj.total_chunks = round(obj.range_length/obj.chunk_length - 0.5); 

      total_chunks = obj.total_chunks; 
     end 

     %% 
     function [total_chunks] = SetChunkLengthInMiliSeconds(obj, chunk_length_ms) 
      % SetChunkLengthInMiliSeconds Defines chunk length for chunk-by-chunk read. 
      % 
      %# [total_chunks] = SetChunkLengthInMiliSeconds(obj, chunk_length_ms) 
      %# INPUT chunk_length_ms: desired chunk length in mili-seconds 
      %# OUTPUT total_chunks: total number of available full chunks 
      % 
      % Abandonds last incomplete chunk. 

      seconds = chunk_length_ms/1000.0; 

      chunk_length_samples = seconds_to_samples(obj, seconds); 

      total_chunks = SetChunkLength(obj, chunk_length_samples); 

     end 
     %% 
     function [chunk, num_read] = ReadChunk(obj) 
      % ReadChunk Reads next chunk of blocklength. 
      % 
      %# [chunk, num_read] = ReadChunk(obj) 
      %# OUTPUT chunk: chunk samples 
      %# OUTPUT num_read: number of read samples. 
      % 
      % If next chunk incomplete: returns zero. 
      % If next chunk not available: returns zero. 

      start_pos = 1 + obj.chunks_read * obj.chunk_length; 
      stop_pos = start_pos + obj.chunk_length -1; 

      if(start_pos>obj.range_length || stop_pos>obj.range_length) 
       num_read = 0; 
       chunk = 0; 
       % warning('Reached the end of samples, nothing read.') 
       % To do: handle last incomplete frame. 
       return; 
      end 

      chunk = obj.range_samples(start_pos:stop_pos); 
      num_read = 1 + stop_pos - start_pos; 

      obj.chunks_read = 1 + obj.chunks_read; 
     end  
     %% 
     function progress = Progress(obj) 
      % Progress Returns progress in percentages. 

      progress = 100.0 * double(obj.chunks_read)/double(obj.total_chunks); 
     end     
     %% 
     function range = GetFullRange(obj) 
      % GetFullRange Returns all samples available. 

      range = obj.range_samples; 
     end 
     %% 
     function fs = GetSamplingRate(obj) 
      % GetSamplingRate Returns sampling rate. 
      fs = obj.fs; 
     end 
     %% 
     function samples = GetNumberOfSamples(obj) 
      samples = obj.total_samples; 
     end 
     %% 
     function num_chunks_read = GetNumberOfReadChunks(obj) 
      % GetNumberOfReadChunks Returns number of chunks read so far. 
      num_chunks_read = obj.chunks_read; 
     end 
     %% 
     function [samples, count] = GetSamples(obj, start_sec, end_sec) 
      % GetSamples Call to immediately get range of samples.    
      [start_sec, end_sec] = obj.checks(start_sec,end_sec); 

      start_sample = obj.seconds_to_samples(start_sec); 
      stop_sample = obj.seconds_to_samples(end_sec);    

      count = 1 + stop_sample - start_sample; 

      try 
       samples = audioread(obj.path, [start_sample, stop_sample]); 
      catch 
       error('Problem with reading wav file. Exiting.') 
      end    
     end 
     %% 
     function [samples, count] = GetAllSamples(obj) 
      % GetAllSamples Call to immediately get all of the samples. 
      try 
       samples = audioread(obj.path); 
      catch 
       error('Problem with reading wav file. Exiting.')      
      end 

      count = obj.total_samples; 
     end   

    end % methods public  

    methods (Access=private) 
     % Secs to samples 
     function [sample] = seconds_to_samples(obj, seconds) 
      sample = obj.fs * seconds; 
     end   

     % Check range 
     function [start_checked, end_checked] = checks(obj, start_sec, end_sec)    

      start_checked = start_sec; 
      end_checked = end_sec; 

      % Check fatal errors 
      if(start_sec>obj.total_seconds ||... 
       start_sec>=end_sec   ||... 
       start_sec<0     ||... 
       end_sec<0)    
       throw(MException('WavReader:FatalError', 'Range error.')); 
      end 

      % Handle overflow 
      if(end_sec > obj.total_seconds) 
       end_checked = obj.total_seconds; 
      end 
     end 
    end % methods private 

end % class 
関連する問題