我可以在 Matlab 中将许多图形保存为批处理位图吗?
Can i save many graphs in Matlab as bitmaps with batch processing?
我希望在 matlab 中分析 .wav 格式的长音频文件,并将整个音频长度导出为 2048x2048 像素位图中的频谱图。对于一秒钟的声音,我希望自动处理大约 20 个位图,并且我想让 matlab 在几个小时内处理 return 1000 个长音频位图,然后在另一个程序中进行进一步处理。
初学者可以吗?
您应该使用命令 print
:
fig = figure();
%Plot some data
print (fig, '-noui', '-dbmp', 'myfile.bmp');
我建议您尝试使用 PNG (png
) 而不是 BMP,因为您可以节省一些内存(或使用 256 个值 bmp bmp256
)
这是一个旧的 SciLab 实现,可以帮助您入门。我建议将生成的频谱图保存为数据,而不是图像。并编写另一个可以将数据转换为图像的函数。
// For simple plotting, Positions in Milliseconds!!
function plotASpectrogram( source, startPosition, endPosition )
[audio,sampleRate] = wavread(source);
startPosition = msecToSamples(startPosition, sampleRate);
endPosition = msecToSamples(endPosition, sampleRate);
audio = cropAudio(audio, startPosition, endPosition );
mapsound(audio);//,.1,100,2000,1,sampleRate);
//Label axes
startPosition = samplesToMsec(startPosition, sampleRate);
if(startPosition==0)
startPosition = "";
else
startPosition = " + " + string(startPosition/1000);
end
labelTheAxes("", "", "Spec " + string(getNameFromSource(source)));
endfunction
// Create a linear spectrogram
// parameter [in] audio - audio file read in with wavread
// parameter [in] specFreqSamples - number of frequencies
// parameter [in] specTimeSamples - number of time samples
// parameter [in] specOverlap - amount of overlap in spectrogram windows
// parameter [in] hann - precalculated hanning window for smoothing
// parameter [out] tempSpec - A matrix containing the spectrogram
function tempSpec = createLinearSpectrogram( audio, specFreqSamples, specTimeSamples, specOverlap, hann )
//Allocate memory for the input and output arrays.
tempSpec = zeros(St,specFreqSamples);
in = zeros(specTimeSamples); //The input will have real values,
out = zeros(specFreqSamples,2); //While the output will have complex values.It is also 1/2 the length of the input array +1.
for i = 1:St
//Store the input data into the input array
for j = 1:specTimeSamples
in(j) = audio(1,(i-1) * specTimeSamples/specOverlap + j);
in(j) = in(j) * hann(j);
end
//Execute the FFT
out(1:specTimeSamples,1) = real(fftw(in(1:specTimeSamples),1));
out(1:specTimeSamples,2) = imag(fftw(in(1:specTimeSamples),1));
//Convert the output into complex data and calculate the absolute value of every
//output. Store it into the spectrogram array.
for j = 1:specFreqSamples
tempSpec(i,j) = sqrt(out(j,1) * out(j,1) + out(j,2) * out(j,2));
end
end
endfunction
// Some utility functions
function labelTheAxes(Title, LabelX, LabelY)
a=get("current_axes");
a.x_label;
a.y_label;
xtitle(string(Title),string(LabelX), string(LabelY));
endfunction
function numberOfSamples = msecToSamples( numberOfMilliSeconds, sampleRate)
numberOfSamples = round((numberOfMilliSeconds / 1000) * sampleRate);
endfunction
function croppedAudio = cropAudio(audio, startPosition, endPosition)
//Parameters in Samples!!!
croppedAudio = audio(1,startPosition:endPosition);
endfunction
我希望在 matlab 中分析 .wav 格式的长音频文件,并将整个音频长度导出为 2048x2048 像素位图中的频谱图。对于一秒钟的声音,我希望自动处理大约 20 个位图,并且我想让 matlab 在几个小时内处理 return 1000 个长音频位图,然后在另一个程序中进行进一步处理。
初学者可以吗?
您应该使用命令 print
:
fig = figure();
%Plot some data
print (fig, '-noui', '-dbmp', 'myfile.bmp');
我建议您尝试使用 PNG (png
) 而不是 BMP,因为您可以节省一些内存(或使用 256 个值 bmp bmp256
)
这是一个旧的 SciLab 实现,可以帮助您入门。我建议将生成的频谱图保存为数据,而不是图像。并编写另一个可以将数据转换为图像的函数。
// For simple plotting, Positions in Milliseconds!!
function plotASpectrogram( source, startPosition, endPosition )
[audio,sampleRate] = wavread(source);
startPosition = msecToSamples(startPosition, sampleRate);
endPosition = msecToSamples(endPosition, sampleRate);
audio = cropAudio(audio, startPosition, endPosition );
mapsound(audio);//,.1,100,2000,1,sampleRate);
//Label axes
startPosition = samplesToMsec(startPosition, sampleRate);
if(startPosition==0)
startPosition = "";
else
startPosition = " + " + string(startPosition/1000);
end
labelTheAxes("", "", "Spec " + string(getNameFromSource(source)));
endfunction
// Create a linear spectrogram
// parameter [in] audio - audio file read in with wavread
// parameter [in] specFreqSamples - number of frequencies
// parameter [in] specTimeSamples - number of time samples
// parameter [in] specOverlap - amount of overlap in spectrogram windows
// parameter [in] hann - precalculated hanning window for smoothing
// parameter [out] tempSpec - A matrix containing the spectrogram
function tempSpec = createLinearSpectrogram( audio, specFreqSamples, specTimeSamples, specOverlap, hann )
//Allocate memory for the input and output arrays.
tempSpec = zeros(St,specFreqSamples);
in = zeros(specTimeSamples); //The input will have real values,
out = zeros(specFreqSamples,2); //While the output will have complex values.It is also 1/2 the length of the input array +1.
for i = 1:St
//Store the input data into the input array
for j = 1:specTimeSamples
in(j) = audio(1,(i-1) * specTimeSamples/specOverlap + j);
in(j) = in(j) * hann(j);
end
//Execute the FFT
out(1:specTimeSamples,1) = real(fftw(in(1:specTimeSamples),1));
out(1:specTimeSamples,2) = imag(fftw(in(1:specTimeSamples),1));
//Convert the output into complex data and calculate the absolute value of every
//output. Store it into the spectrogram array.
for j = 1:specFreqSamples
tempSpec(i,j) = sqrt(out(j,1) * out(j,1) + out(j,2) * out(j,2));
end
end
endfunction
// Some utility functions
function labelTheAxes(Title, LabelX, LabelY)
a=get("current_axes");
a.x_label;
a.y_label;
xtitle(string(Title),string(LabelX), string(LabelY));
endfunction
function numberOfSamples = msecToSamples( numberOfMilliSeconds, sampleRate)
numberOfSamples = round((numberOfMilliSeconds / 1000) * sampleRate);
endfunction
function croppedAudio = cropAudio(audio, startPosition, endPosition)
//Parameters in Samples!!!
croppedAudio = audio(1,startPosition:endPosition);
endfunction