- a code for general spectral analysis
- examples of digital filters you can use to eliminate unwanted frequency bands or keep (with a bandpass filter) frequencies of interest; once the filter is designed you can apply it on your signal using filter or filtfilt
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to filter all the frequencies from the audio signal.
14 views (last 30 days)
Show older comments
Hello guys, so I have this problem, I do have an audio signal, from two seperate sources with some noise. I need to seperate thoose into proper frequencies and print them out, how would I do that ? I already have the spectrum of the signal, that i acquired using FFT. But i get a lot of frequencies grouped in two spots, i don't get it how i get only the frequencies i need ? I added picture of the spectrum above.
Accepted Answer
Mathieu NOE
on 24 Nov 2020
hello
2 gifts for you today (lucky man !)
hope it helps
36 Comments
Justas Kvederis
on 24 Nov 2020
Thanks, will try to review them to the best of my ability and get back at you, but already a massive thanks for the code and help.
Mathieu NOE
on 24 Nov 2020
so design a bandpass filter which frequency range match the group of frequencies to keep
and apply it to you input signal
Justas Kvederis
on 24 Nov 2020
Well I am familiar with filters, but how do i know which frequencies to filter which to keep all i have is a file with, signal values and this code.
clear all; close all;
%-- Td - diskretizavimo periodas =125Hz
%-- N2 - is laikmenos nuskaitytu reiksmiu skaicius
Fd = 24000;
Td = 1/Fd;
fid = fopen('G4_5.dat','r')
[signalas,N2] = fscanf(fid, '%12f');
fclose(fid);
figure(1)
Y= abs(fft(signalas,N2));
figure(1); stem(Y,'.-');
Mathieu NOE
on 24 Nov 2020
So this is an example with a notch filter centered on the first peak around 88 Hz (to remove)
of course you can apply whatever filter if your aim is different ... use this as an example
also there was an error when you do fft; you should only pick the first half of the fft points , the second half is only a mirror image (can be immediately recognized on your first picture)
look in documentation / fft / ('Single-Sided Amplitude Spectrum of S(t)')
so , the code :
clc
clear all
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FFT parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NFFT = 1024*4; %
NOVERLAP = round(0.75*NFFT);
w = hanning(NFFT); % Hanning window / Use the HANN function to get a Hanning window which has the first and last zero-weighted samples.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% load signal
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% test data
Fs = 24000;
% t = 0:1/Fs:10-1/Fs;
fid = fopen('G4_5.dat','r')
[signal,samples] = fscanf(fid, '%12f');
fclose(fid);
%% notch filter section %%%%%%
% H(s) = (s^2 + 1) / (s^2 + s/Q + 1)
fc = 88; % notch freq
wc = 2*pi*fc;
Q = 3; % adjust Q factor for wider (low Q) / narrower (high Q) notch
% at f = fc the filter has gain = 0
w0 = 2*pi*fc/Fs;
alpha = sin(w0)/(2*Q);
b0 = 1;
b1 = -2*cos(w0);
b2 = 1;
a0 = 1 + alpha;
a1 = -2*cos(w0);
a2 = 1 - alpha;
% digital notch
num1z=[b0 b1 b2];
den1z=[a0 a1 a2];
freq = linspace(fc-1,fc+1,200);
[gd1,pd1] = dbode(num1z,den1z,1/Fs,2*pi*freq);
gd1db = 20*log10(gd1);
figure(1);
plot(freq,gd1db,'b');
title(' Notch: H(s) = (s^2 + 1) / (s^2 + s/Q + 1)');
xlabel('Frequency (Hz)');
ylabel(' dB')
% now let's filter the signal
signal_filtered = filtfilt(num1z,den1z,signal);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% display : averaged FFT spectrum before / after notch filter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[sensor_spectrum, freq] = pwelch(signal,w,NOVERLAP,NFFT,Fs);
sensor_spectrum_dB = 20*log10(sensor_spectrum);% convert to dB scale (ref = 1)
[sensor_spectrum_filtered, freq] = pwelch(signal_filtered,w,NOVERLAP,NFFT,Fs);
sensor_spectrum_filtered_dB = 20*log10(sensor_spectrum_filtered);% convert to dB scale (ref = 1)
figure(2),semilogx(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 notch filter','after notch filter');
xlabel('Frequency (Hz)');ylabel(' dB')
if you're happy with it please accept my answer
thanks
Justas Kvederis
on 24 Nov 2020
Thanks for the effort and the help you provided, I will accept your answer since you really helped me a lot at least to grasp the concept.
Justas Kvederis
on 25 Nov 2020
Hey sorry to bother you again, but this signal has noise, how do i get rid of the noise only ? And get a pure audio signal mix, Because that signal is a mix of 5 audio frequencies and noise. Bassically i need to get 5 values of frequencies at the end.
Mathieu NOE
on 25 Nov 2020
no problem
I could identify clearly 4 tones , but the fifth is where ? burried in the noise ?
from there we can use narrow bandpass filters (and put them in paralle) to keep the tones and reduce the noise
for the time being , this is just the code slightly modified with decimation to speed up things (as there is not much info in the signal above 1000 Hz)
clc
clear all
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FFT parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NFFT = 1024; %
NOVERLAP = round(0.75*NFFT);
w = hanning(NFFT); % Hanning window / Use the HANN function to get a Hanning window which has the first and last zero-weighted samples.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% load signal
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% test data
Fs = 24000;
% t = 0:1/Fs:10-1/Fs;
fid = fopen('G4_5.dat','r')
[signal,samples] = fscanf(fid, '%12f');
fclose(fid);
% decimate
decim = 12;
signal = decimate(signal,decim);
Fs = Fs/decim;
%% notch filter section %%%%%%
% H(s) = (s^2 + 1) / (s^2 + s/Q + 1)
fc = 88; % notch freq
wc = 2*pi*fc;
Q = 3; % adjust Q factor for wider (low Q) / narrower (high Q) notch
% at f = fc the filter has gain = 0
w0 = 2*pi*fc/Fs;
alpha = sin(w0)/(2*Q);
b0 = 1;
b1 = -2*cos(w0);
b2 = 1;
a0 = 1 + alpha;
a1 = -2*cos(w0);
a2 = 1 - alpha;
% digital notch
num1z=[b0 b1 b2];
den1z=[a0 a1 a2];
freq = linspace(fc-1,fc+1,200);
[gd1,pd1] = dbode(num1z,den1z,1/Fs,2*pi*freq);
gd1db = 20*log10(gd1);
figure(1);
plot(freq,gd1db,'b');
title(' Notch: H(s) = (s^2 + 1) / (s^2 + s/Q + 1)');
xlabel('Frequency (Hz)');
ylabel(' dB')
% now let's filter the signal
signal_filtered = filtfilt(num1z,den1z,signal);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% display : averaged FFT spectrum before / after notch filter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[sensor_spectrum, freq] = pwelch(signal,w,NOVERLAP,NFFT,Fs);
sensor_spectrum_dB = 20*log10(sensor_spectrum);% convert to dB scale (ref = 1)
[sensor_spectrum_filtered, freq] = pwelch(signal_filtered,w,NOVERLAP,NFFT,Fs);
sensor_spectrum_filtered_dB = 20*log10(sensor_spectrum_filtered);% convert to dB scale (ref = 1)
figure(2),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 notch filter','after notch filter');
xlabel('Frequency (Hz)');ylabel(' dB')
Mathieu NOE
on 25 Nov 2020
code updated with multiple band pass filters implementation (butterworth)
if you find the missing frequency , you can add that to the list and run the code
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FFT parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NFFT = 1024; %
NOVERLAP = round(0.75*NFFT);
w = hanning(NFFT); % Hanning window / Use the HANN function to get a Hanning window which has the first and last zero-weighted samples.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% load signal
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% test data
Fs = 24000;
% t = 0:1/Fs:10-1/Fs;
fid = fopen('G4_5.dat','r')
[signal,samples] = fscanf(fid, '%12f');
fclose(fid);
% decimate
decim = 12;
signal = decimate(signal,decim);
Fs = Fs/decim;
% %% notch filter section %%%%%%
% % H(s) = (s^2 + 1) / (s^2 + s/Q + 1)
%
% fc = 88; % notch freq
% wc = 2*pi*fc;
% Q = 3; % adjust Q factor for wider (low Q) / narrower (high Q) notch
% % at f = fc the filter has gain = 0
%
% w0 = 2*pi*fc/Fs;
% alpha = sin(w0)/(2*Q);
%
% b0 = 1;
% b1 = -2*cos(w0);
% b2 = 1;
% a0 = 1 + alpha;
% a1 = -2*cos(w0);
% a2 = 1 - alpha;
%
% % digital notch
% num1z=[b0 b1 b2];
% den1z=[a0 a1 a2];
% keep only signal content extracted by bandpass filters
fc = [89 291 390 640]; % Hz
bandwith = 10; % Hz
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)]);
freq = linspace(10,1000,100);
[gd1,pd1] = dbode(b,a,1/Fs,2*pi*freq);
gd1db = 20*log10(gd1);
figure(1);
plot(freq,gd1db);
title(' Bandpass');
xlabel('Frequency (Hz)');
ylabel(' dB')
hold on
% now let's filter the signal and sum up with previous iteration
signal_filtered = signal_filtered + filtfilt(b,a,signal);
end
hold off
% freq = linspace(fc-1,fc+1,200);
% [gd1,pd1] = dbode(num1z,den1z,1/Fs,2*pi*freq);
% gd1db = 20*log10(gd1);
%
% figure(1);
% plot(freq,gd1db,'b');
% title(' Notch: H(s) = (s^2 + 1) / (s^2 + s/Q + 1)');
% xlabel('Frequency (Hz)');
% ylabel(' dB')
%
% % now let's filter the signal
% signal_filtered = filtfilt(num1z,den1z,signal);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% display : averaged FFT spectrum before / after filters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[sensor_spectrum, freq] = pwelch(signal,w,NOVERLAP,NFFT,Fs);
sensor_spectrum_dB = 20*log10(sensor_spectrum);% convert to dB scale (ref = 1)
[sensor_spectrum_filtered, freq] = pwelch(signal_filtered,w,NOVERLAP,NFFT,Fs);
sensor_spectrum_filtered_dB = 20*log10(sensor_spectrum_filtered);% convert to dB scale (ref = 1)
figure(2),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')
Justas Kvederis
on 1 Dec 2020
Hey, thanks for all the help, probably just one last thing, I need to find the frequencies of the signal automatically, and you found them manually, how could I accomplish that ?
Mathieu NOE
on 1 Dec 2020
you can use the function findpeaks
see help findpeaks
as we are looking for narrow peaks , you have to use arguments like 'MaxPeakWidth'
[...] = findpeaks(...,'MaxPeakWidth',MAXW)
Justas Kvederis
on 1 Dec 2020
Thanks for this one, but i still need to find width automatically, I can't enter a single digit myself that isn't calculation, how would I do that ?
Justas Kvederis
on 1 Dec 2020
Also since, my signal is really uneven, it finds peaks even in noise signal.
Mathieu NOE
on 1 Dec 2020
ok
this is an attempt to make a better code - automatic also
but there will always be some situations where you have to adapt it to your signal
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% load signal
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% test data
Fs = 24000;
% t = 0:1/Fs:10-1/Fs;
fid = fopen('G4_5.dat','r')
[signal,samples] = fscanf(fid, '%12f');
fclose(fid);
% decimate
decim = 12;
signal = decimate(signal,decim);
Fs = Fs/decim;
samples = length(signal);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FFT parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n_max = fix(log2(samples));
NFFT = 2^(n_max-1); % 2^n with n chosen so that NFFT is below length of signal (even after decimation) by factor > 2 so some averaging is doable
NOVERLAP = round(0.75*NFFT);
w = hanning(NFFT); % Hanning window / Use the HANN function to get a Hanning window which has the first and last zero-weighted samples.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% display : averaged FFT spectrum before filter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[sensor_spectrum, freq] = pwelch(signal,w,NOVERLAP,NFFT,Fs,'power');
sensor_spectrum_dB = 10*log10(sensor_spectrum);% convert to dB scale (ref = 1)
% sensor_spectrum_dB = sensor_spectrum_dB-min(sensor_spectrum_dB); % y axis shift so dB values are positive (min value = 0)
%% findpeaks
df = Fs/NFFT;
MAXW = 5*df;
MPD = MAXW;
MPP = 20;
[peaks,loc,W,P] = findpeaks(sensor_spectrum_dB,'MinPeakDistance',MPD,'MaxPeakWidth',MAXW,'MinPeakProminence',MPP);
freq_peaks = freq(loc);
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')
% %% notch filter section %%%%%%
% % H(s) = (s^2 + 1) / (s^2 + s/Q + 1)
%
% fc = 88; % notch freq
% wc = 2*pi*fc;
% Q = 3; % adjust Q factor for wider (low Q) / narrower (high Q) notch
% % at f = fc the filter has gain = 0
%
% w0 = 2*pi*fc/Fs;
% alpha = sin(w0)/(2*Q);
%
% b0 = 1;
% b1 = -2*cos(w0);
% b2 = 1;
% a0 = 1 + alpha;
% a1 = -2*cos(w0);
% a2 = 1 - alpha;
%
% % digital notch
% num1z=[b0 b1 b2];
% den1z=[a0 a1 a2];
% keep only signal content extracted by bandpass filters
fc = freq_peaks; % Hz
bandwith = 10; % Hz
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)]);
freq = linspace(10,1000,100);
[gd1,pd1] = dbode(b,a,1/Fs,2*pi*freq);
gd1db = 20*log10(gd1);
figure(2);
plot(freq,gd1db);
title(' Bandpass');
xlabel('Frequency (Hz)');
ylabel(' dB')
hold on
% now let's filter the signal and sum up with previous iteration
signal_filtered = signal_filtered + filtfilt(b,a,signal);
end
hold off
% freq = linspace(fc-1,fc+1,200);
% [gd1,pd1] = dbode(num1z,den1z,1/Fs,2*pi*freq);
% gd1db = 20*log10(gd1);
%
% figure(1);
% plot(freq,gd1db,'b');
% title(' Notch: H(s) = (s^2 + 1) / (s^2 + s/Q + 1)');
% xlabel('Frequency (Hz)');
% ylabel(' dB')
%
% % now let's filter the signal
% signal_filtered = filtfilt(num1z,den1z,signal);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% display : averaged FFT spectrum before / after notch filter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [sensor_spectrum, freq] = pwelch(signal,w,NOVERLAP,NFFT,Fs,'power');
% sensor_spectrum_dB = 10*log10(sensor_spectrum);% convert to dB scale (ref = 1)
[sensor_spectrum_filtered, freq] = pwelch(signal_filtered,w,NOVERLAP,NFFT,Fs,'power');
sensor_spectrum_filtered_dB = 10*log10(sensor_spectrum_filtered);% convert to dB scale (ref = 1)
figure(3),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')
Justas Kvederis
on 1 Dec 2020
I know I might sound like an idiot, but I need some more assistance, honestly this is beoyind me, but my task is something like this. Get all the audio frequencies (which is collected from two sources at same time) from the spectrum, print our their frequencies. After that there are many more tasks, but go figure my lecturer advised using short time fourier transformation. Which you are clearly not using, there is no point in me filtering any of this, and my entire beggining wasn't correct, also your code gives incorrect values, but thats not the point. Could I request a simple code snippet where you get frequencies from function spectrogram(signal, sampling rate) (additional parameters are okay) or stft. I can probably see the values (at least i think i do) when i use the command spectrogram(signalas,blackman(500),100,150,Fd); signalas=signal Fd=fs, I am assuming thoose green lines are voice signals. How do i extract them ?
Mathieu NOE
on 2 Dec 2020
hello
no probelm
FYI, the green lines in the code are commented code that I sometimes leave intentionnally because they can show how to do (for example) filtering of audio signal
if you don't want to see those lines, you can delete them - this will have no impact on the rest of the code;
FYI also , a spectrogram is a short time fourier transformation : data is splitted in overlapping sections that are then processed by fft; this is the definition of short time fourier transformation
so I don't know precisely what you want in terms of "frequencies" ? at the beginning it was about the "tones" (and get rid of the broadband noise) but now I beleive you want something else
you see the plots for the averaged spectrum and from the spectrogram in my code
so which info about frequencies is needed ? still unclear at this point ?
I also wondre why you use these values in the parameters for spectrogram : blackman(500),100,150
Justas Kvederis
on 2 Dec 2020
Well just to clarify, I know what comments are, I was asking what are thoose green lines In spectogram (there are 4 green lines trough the spectogram). Well Now i need to get all the frequencies of the audio signal, so in total 4 freqencies: 100, 318, 427, 702, need to get them automatically. Also in the beggining I thought i can filter out the noise, and find the audio signals easier that way, I just figured out filtering noise isn't effective or smart way to do it.
Mathieu NOE
on 2 Dec 2020
well
i don't see any green lines in the spectrogram (added back that in the code)
if yiu have audio buried in noise , unless you know if they are in distinct frequency range , and in that case you can simply use bandpass / bandstop filters, but if they are in same frequency range this will be much ore difficult ; it also depends of the nature of the noise (tones / random noise ...)
Justas Kvederis
on 2 Dec 2020
I was talking about theese green lines. That you can see in the picture (4 of them). But thanks for all the help I will try to figure it out somehow, I am glad this is just a side module not a main one or I would be toast.
Mathieu NOE
on 2 Dec 2020
ok
thse lines are related to transients in your signal; for example if you have a short pulse , a shock , or a discontinuity; this will have a large frequency content over a short time period
the lines appears vertical in my graph because we have not the same time / frequency axis orientation
Justas Kvederis
on 2 Dec 2020
can I somehow extract their frequencies, arent thoose short impulses exactly what I am searching for ?
Mathieu NOE
on 2 Dec 2020
this will not be one or a few frequencies , it's almost a continuous spectrum
remembet the fft of an impulse is a flat spectrum (no peaks)
Laurynas Buinauskas
on 3 Dec 2020
In your latest code, you use the function decimate(). Is there a way to find those 4 tones without using that function, in other words working with the same sampling rate. Also, with decimation, you lose some information from the signal and lose accuracy while trying to find the tones. Of course, the difference might not be that big, but still, the accuracy might be lost. So, I was just wondering why did you use decimate. Also, could you explain a little bit about the algorithm in your code, and why did you choose certain parameters (MAXW, MPD, MPP, n_max, NFFT, NOVERLAP, and so on). By the way, I'm also a novice, so I don't understand much in signal processing. So, any explanation would be great.
Mathieu NOE
on 3 Dec 2020
hi
decimate is usefull when the frequency range of interest ist much lower than Fs/2 (which is also called the Nyquist frequency).
if you don't want to have decimation, simply put 1 as value and there will be no more decimation;
and I may not agree about resolution; decimation does not reduce resolution, it reduces the amount of fft computation (therefore the computation time).
NB : the frequency resolution is the same if nfft and Fs are modified by the same factor (= decimation factor).
for the parameters like MAXW, MPD, MPP I had to try experimentaly, because I didn't find a mathematical robust way to link those parameters to other fft parameters. sorry about that.
for NFFT, NOVERLAP , you have to get more familiar with fft averaging techniques ; it's fairly common use in fft spectral analysis ; it's probably to technique that is mostly used in all vibration & noise FFT analysers.
if you google fft averaging, you will get lot of information about signal processing and fft techniques. You can also read vendor's brochures.
attached a usefull reminder about a few topics related to signal processing
Laurynas Buinauskas
on 5 Dec 2020
Sorry to bother you again, but I have a question. Now that we know the tones of the 4 signals, how can we find the exact time when they have started and also how long they have lasted?
Mathieu NOE
on 6 Dec 2020
hello
no problem
this code will show how to compute the start and stop time of the envelope of the signal
Justas Kvederis
on 7 Dec 2020
Hello again, look I am going to admit, your code is wayyyy too difficult for me. And even if it wasn't that difficult I can't just reuse your code, so I am asking for the simplest approach to this.
My task is to find the starting and ending points when they are distributed like this.
Basically I have 4 sine waves mixed with noise, and now I need to find start point and end point to each of them. But I am really struggling with your code no matter how many times I look at it, for example I find my frequencies with simple for loop:
for i=1:length(Y)
if Y(i) > 0
daugiau_uz = daugiau_uz + 1;
nuliai = 0;
else
nuliai = nuliai + 1;
end
if daugiau_uz > maziausias_plotis
j = 0;
sk = daugiau_uz;
while sk >= 1
if Y(i - j) > 0
klonas_blogu(i - j) = Y(i - j);
sk = sk - 1;
end
j = j + 1;
end
end
if nuliai > 5
daugiau_uz = 0;
end
end
This is only a snippet, but what I did i just made all spectrum values below 1000 = 0 (Y(Y<1000)=0;) and in loop I search for 5 zeros in a row in the spectrum, if I Find it I know it's either noise or sine wave, but since I know that sine wave will allways be thinner than noise, I just take 4 thinnest values and thoose are my frequencies. Well that's not the point I need something as simple as this, a loop a few simple functions, and that's it, right now I am attempting something like this:
piece = 500;
tro = zeros(1,N2);
for i=1:piece:length(signalas)-piece
tro(1:piece) = signalas(i:i+piece-1);
tem = abs(fft(tro,piece));
if tem(318) > 5
disp(i);
T=(1/Fd)*piece;
delf=1/T;
daznAs=0:delf:Fd-delf;
stem(daznAs,tem, 'k.-');
end
end
I know that this is incorrect, but what I tried to achieve is I split my signal piece by piece(size is 500) then i calculate spectrum for every piece and try to see if my desired frequency (this time 318) shows up, if it does I calculate the time when it show's up. How do I make it work, or do you know better solution ?
Mathieu NOE
on 7 Dec 2020
hello
well , you could follow that logic on the spectrogram plot , because you are doing the same : take the fft of small time buffers, look at fft amplitudes at specific frequencies and decide is signal is low or high and then compute start and stop time
so this is a frequency domain approach
my initial approach was based on simply take the enveloppe of the (time) signal after each bandpass filter
it's very easy
Justas Kvederis
on 7 Dec 2020
Thanks, for quick response, is there any way that you could, maybe show me with the code I am doing just with the loop, I don't require any filtering. 100 318 427 702 ( 91 290 390 641) I have thoose frequencies, Now what amount of time should I take, and how do I properlly convert it to real frequencies, since I am confused. And I am aware this is easy, It's just goes right trough me I probably missed something important with the basics. I only need pure numbers start time and end time of each of the frequency (sine wave) no need for filtering etc.
Justas Kvederis
on 7 Dec 2020
Main issue I am having is, if i take only part of the spectrum it get's distorted, and I have no idea how to check if frequency showed up, I can't just write if y.value>5 or something since it could be noise, it could be value really unsure what to do here.
Mathieu NOE
on 8 Dec 2020
I tried to compare frequency domain approaches either based on spectrogram or stft (short time fourier transform).
basically the plot results are the same (if nfft and overlap are identical in both simulations)
I found a bit difficult to generate a robust method to detect start and stop time in the spectrogram plot
also the time resolution is not best due to trade offs between frequency resolution and time resolution in stft .
again, IMHO, the best approach is to use the defined bandpass filters, to look at the time signal once bandpass filtered , to take envelope of that and then to detect the start / stop times based on when the envelope is above a certain threshold. That's what I already showed you
I don't see a third option here , sorry
Justas Kvederis
on 8 Dec 2020
Hey, thanks for all the help you already done more than you should have. Since my deadline is tomorrow i will be trying to accomplish this now, thanks for everything. I hope I won't need to bother you again.
Mathieu NOE
on 8 Dec 2020
ok - not everything goes smooth and shinny as we would like too
I just hope you are not too much annoyed with signal processing !!
More Answers (0)
See Also
Categories
Find more on Frequency Transformations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)