0
votes

I am looking to analyze sounds from a machine with a microphone connected to a DAQ board to collect data. The data is stored in an excel file and then I want to read that data in MATLAB and perform a FFT to see what frequencies are causing the greatest amplitude of noise. To test my system I fed some known frequencies into the DAQ through a function generator. However, when I feed a 400Hz signal from the function generator to the DAQ, collect the data, upload it into MATLAB and plot it, I do not see any tones at 400 Hz, only some at higher frequencies. Is this a problem with my code?

y = xlsread('TrialExcel400HzOscilloscope.xlsx');      
y = detrend(y);
n = length(y);

Fs = 48000;                 
nfft = 4096;                
Ts = 1/Fs;                  
t = 0:Ts:(n*Ts)-Ts;        

numUniq = ceil((nfft+1)/2); 
f = (0:numUniq-1)'*Fs/nfft; 



figure(1)                   
hAx(1) = subplot(211);
hLine(1) = line('XData',t,'YData',nan(size(t)), 'Color','b', 'Parent', hAx(1));
xlim([0 0.05]);            
ylim([-10 10]);              
xlabel('Time (s)');
ylabel('Amplitude (Volts)');
title('Time Domain');

hAx(2) = subplot(212);
hLine(2) = line('XData', f, 'YData', nan(size(f)), 'Color', 'b', 'Parent', hAx(2));
grid
xlim([0 25000]);           
ylim([0 80]);               
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
title('Frequency Domain');

a = 20*log10(abs(fft(y,nfft)));                                   


set(hLine(1), 'YData',y);                                              
set(hLine(2), 'YData', a(1:numUniq));      

output graph of 400Hz function generator tone

1
What is n? Is it length(y)? How does that compare to nfft? Because what you are doing here is computing the DFT of the first nfft samples in your signal, not the first nfft frequencies in your signal. I would recommend setting nfft=length(y) instead.Cris Luengo
yes n is length(y) I just forgot to define it! It appears I was a bit redundant because nfft and length(y) are the same value. The DAQ only returns 4096 points.Alyssa Kelly

1 Answers

0
votes

"I fed some known frequencies into the DAQ through a function generator" -- Did it work OK for other frequencies then?

The peaks you are seeing might be noise in the DAG or an issue with the frequency generator.

Considering your signal is a little under 0.1 seconds, I expect to see about 40 periods of a sine wave in it (actually, since you plot only 0.05 s, it would be exactly 20 periods in your graph). This is obviously not the case with this signal.

Your code for computing and plotting the magnitude of the spectrum is OK.