Need a little help on my Spectrum Analyzer. It's pretty much done but I'm stuck.

2 vues (au cours des 30 derniers jours)
Colin
Colin le 25 Avr 2014
Modifié(e) : Image Analyst le 25 Avr 2014
Hi, I'm new to MatLab and I'm trying to build a spectrum analyzer. I think I have most of it but I can't get it to work. I was hoping someone could glance at it to see if it was something obvious. Thanks for any help.
The script is at the bottom
This is the function:
function y = CPetty_spectrumAnalyzer( filename, winLength, overlapLength, window, fftlength)
%Input
%filename: name of .WAV file to be analyzed
%winLength: length of signal window (in samples)
%overlapLength: length of signal overlap (in samples)
%window: window type: passes one of the following strings
% ‘rect’
% ‘hamming’
% ‘hann’
% ‘blackman’
% ‘bartlett’
%fftlength: number of samples to be used in fft (default: winLength)
%Output
%y : a 2D matrix containing the normalized magnitudes of the STFT in dB
%If the user did not input 5 arguments total...
if (nargin ~= 5);
disp('You have to enter 5 arguments (filename,winLength,overlapLength,window,fftlength ')
end
%If the user trys to name the file with numbers and symbols
if ~isstr(filename)
error('The filename has to be letters with single quotes around it')
end
%If the overlap value is greater than the window length.
if (overlapLength > winLength)
error('The overlap value cant be longer than the window length')
end
%Since the fftlength uses the samples in the windows, it can't be less
%than the window length.
if fftlength < winLength,
error('The fftlength cant be less than the winLength')
end
if (winLength < 1), abs(winLength);
disp('You cant use negative numbers, I made them positive for you')
end
if (overlapLength < 1), abs(overlapLength);
disp('You cant use negative numbers, I made them positive for you')
end
if (fftlength < 1), abs(fftlength);
disp('You cant use negative numbers, I made them positive for you')
end
if (mod(winLength, 1) ~= 0)||(mod(overlapLength, 1) ~= 0)||(mod(fftlength, 1) ~= 0);
disp('Lengths cant be floating points. I rounded them up to the next integer')
end
switch window
case 'rect'
win = rect(winLength);
case 'hamming'
win = hamming(winLength);
case 'hann'
win = hann(winLength);
case 'blackman'
win = blackman(winLength);
case 'bartlett'
win = bartlett(winLength);
otherwise
error('rect, hamming, hann, blackman or bartlett');
end
%Load the file and store the audio in x and the samplerate in fs.
[x, fs] = wavread(filename);
%To make a stereo file mono, the mean is calculated.
x = mean(x, 2);
%Make the hop size the difference between the window and overlap lengths.
hop = winLength - overlapLength;
%To buffer the signal to the right window and overlap lengths.
buf = buffer(x, winLength, overlapLength);
%K is the number of frames that are needed for the function.
K = ceil(length(x)/hop);
%To create a matrix by tiling.
winMat = repmat(win, 1, K);
%Matrix multiplication for each frame of audio by each window function.
winBuf = buf .* winMat;
%FFT of the windowed signal.
fftBuf = fft(winBuf, fftlength);
%Find the normalized magnitudes of the post STFT signal in Db.
magnitudes = 20*log10(abs(fftBuf) / (fftlength/2));
%Find the ammount of information that's in the bin.
NyqBin = (length(magnitudes)/2) + 1;
end
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
y = CPetty_spectrumAnalyzer('testSig.wav', 1024, 512, 'hann');
[x, fs] = wavread('testSig.wav');
dur = length(x) / fs;
time = linspace( 0, dur, size(y, 2) );
freqs = linspace( 0, fs/2, size(y,1)+1 );
imagesc(time, freqs, y);
axis( 'xy' )
xlabel('Time (s)');
ylabel('Frequency (Hz)');
title('Spectogram');
colormap('Autumn')
f = figure;
movegui(f,'center');
imagesc(time, freqs, y);
axis( 'xy' ) %Flip axes
xlabel('Time (s)');
ylabel('Frequency (Hz)');
ylim([0 8000]); % Limit y axis to 8kHz
title('Spectogram result for 0 - 8kHz.')
colormap('Autumn');

Réponses (0)

Catégories

En savoir plus sur Measurements and Feature Extraction dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by