3
votes

I'm trying to get a frequency response curve from a microphone that I have connected to my pc, using matlab.

I think I'm pretty close of getting the final code, but i think I'm missing something.

This is what I have right now:

close all, clear all, clc

x = 5;                        % seconds recording
Fs = 44100;                   % Sampling frequency
T = 1/Fs;                     % Sample time                     
L= x*1000;                    % Length of signal
t = (0:L-1)*T;                % Time vector


% Record your voice for 'x' seconds.
recObj = audiorecorder(Fs, 24, 1);
disp('Start of Recording.');
recordblocking(recObj, x);
disp('End of Recording.');
% Store data in double-precision array.
myRecording = getaudiodata(recObj);

NFFT = 2^nextpow2(L); % Next power of 2 from length of y
fourier = fft(myRecording);
Y = fft(myRecording,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
X = 2*abs(Y(1:NFFT/2+1));
samples = get(recObj,'TotalSamples');

plot(f,X)  
title('Single-Sided Amplitude Spectrum)
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')

This part of the code is correct I think. For example, when I play a tone of 5kHz I get this plot: this

Now I play pink noise, and add this small part of code to convert it to dB, so I can get the frequency response curve:

dbX = db(X);
plot(f,dbX)

I expect (or my goal is..) a frequency response curve (as you can find on google images for example, I don't have enough reputation for more than 2 links, so sorry I didn't use a picture link here) , but I got this instead:

this

Clearly I'm doing something wrong, but I don't know what ..

2
Hum, I don't know much about this, but, why is that pink noise frequency plot wrong? what are you expecting?Ander Biguri
My goal is to get something like this: acoustics.salford.ac.uk/acoustics_info/microphones/… Mine isn't even around 0dB, or not a flat line or something ..Eric_W
That would be just a matter of scaling, the signal. Also note that the plot says 'relative response'.mpaskov

2 Answers

0
votes

You are closer than you think. Here are three tips to get a little closer.

Firstly, you need a logarithmic plot of your frequency domain data. Use semilogx() instead of plot.

Secondly, you're going to need to smooth the data. The simplest function for this in Matlab is smooth() but there are more advanced options that may better suit your needs.

Finally, in order to get a relative response, subtract the mean from your data. dbx_relative = dbx-mean(dbx)

Putting it all together:

dbX = db(X); 
relative_dbx = dbx-mean(dbx); 
smoothed_dbx = smooth(relative_dbx); 
semilogx(f,smoothed_dbx);
0
votes

Use pwelch to compute the transfer function between the stimulus signal (i.e. the reference audio waveform ) and the response (what you measure with your microphone).