spectrogram_dB_scale = 60;
Fs = 24000;
fid = fopen('G4_12.dat','r')
[signal,samples] = fscanf(fid, '%12f');
fclose(fid);
decim = 12;
signal = decimate(signal,decim);
Fs = Fs/decim;
samples = length(signal);
time =(1:samples)/Fs;
n_max = fix(log2(samples));
NFFT = 2^(n_max-1);
NOVERLAP = round(0.95*NFFT);
w = hanning(NFFT);
[sensor_spectrum, freq] = pwelch(signal,w,NOVERLAP,NFFT,Fs,'power');
sensor_spectrum_dB = 10*log10(sensor_spectrum);
freq_peaks = [159.0574, 359.3519, 459.4992, 709.8674];
peaks = interp1(freq,sensor_spectrum_dB,freq_peaks,'linear');
figure(1),plot(freq,sensor_spectrum_dB,'b',freq_peaks,peaks,'*r');grid
title(['Averaged FFT Spectrum / Fs = ' num2str(Fs) ' Hz / Delta f = ' num2str(freq(2)-freq(1)) ' Hz ']);
xlabel('Frequency (Hz)');ylabel(' dB')
[sg,fsg,tsg] = specgram(signal,NFFT,Fs,hanning(NFFT),NOVERLAP);
sg_dBpeak = 20*log10(abs(sg))+20*log10(2/length(fsg));
min_disp_dB = round(max(max(sg_dBpeak))) - spectrogram_dB_scale;
sg_dBpeak(sg_dBpeak<min_disp_dB) = min_disp_dB;
figure(2);
imagesc(tsg,fsg,sg_dBpeak);colormap('jet');
axis('xy');colorbar('vert');grid
title(['Spectrogram / Fs = ' num2str(Fs) ' Hz / Delta f = ' num2str(fsg(2)-fsg(1)) ' Hz ']);
xlabel('Time (s)');ylabel('Frequency (Hz)');
fc = freq_peaks;
bandwith = 8;
f_low = fc-0.5*bandwith;
f_high = fc+0.5*bandwith;
N = 4;
signal_filtered = zeros(size(signal));
for ci = 1:length(fc)
[b,a] = butter(N,2/Fs*[f_low(ci) f_high(ci)]);
tmp = filtfilt(b,a,signal);
signal_filtered = signal_filtered + tmp;
end
[sensor_spectrum_filtered, freq] = pwelch(signal_filtered,w,NOVERLAP,NFFT,Fs,'power');
sensor_spectrum_filtered_dB = 10*log10(sensor_spectrum_filtered);
figure(4),plot(freq,sensor_spectrum_dB,'b',freq,sensor_spectrum_filtered_dB,'r');grid
title(['Averaged FFT Spectrum / Fs = ' num2str(Fs) ' Hz / Delta f = ' num2str(freq(2)-freq(1)) ' Hz ']);
legend('before filter','after filter');
xlabel('Frequency (Hz)');ylabel(' dB')
figure(5),plot(time,signal_filtered,'b');grid
title('Filtered signal');
xlabel('Time (s)');ylabel(' amplitude')
Best Answer