Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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

Status
Not open for further replies.

irrelevant

Newbie level 1
Joined
Apr 25, 2014
Messages
0
Helped
0
Reputation
0
Reaction score
0
Trophy points
0
Activity points
0
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');
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top