EEGLAB Ways to 60Hz Remove Line Noise and Harmonics - matlab

I am attempting to remove line noise and its harmonics from many EEG datasets in EEGLAB. I have been using the CleanLine plugin to do this. However, it does not work well sometimes for some datasets. For example, here are the log power spectral density plots before and after cleanline.
Before cleanline:
.
After cleanline:
The code that I used is:
EEG = pop_cleanline(EEG, 'bandwidth',2,'chanlist',[1:68] ,'computepower',1,'linefreqs',[60:60:420] ,'newversion',0,'normSpectrum',0,'p',0.01,'pad',2,'plotfigures',0,'scanforlines',0,'sigtype','Channels','taperbandwidth',2,'tau',100,'verb',1,'winsize',4,'winstep',1);
I have also tried to use a notch filter with a lower edge of 55 and an upper edge of 65, but that does not work well.
Are there any suggestions on what other method of removing line noise and harmonics that I could try? Or is CleanLine the best method? Are there ways we can adjust the parameters of cleanline to allow it to work better?

You could use the ica function followed by IClable and delete all the line noise components. If you would go that way, it might be better not leave the clean_line function. I believe that ICA is more likely to create pure line noise components like that.

Have you applied high pass filter of 0.5 Hz already? Consider doing that first, then try cleanline with tweaked parameters (the ones that give the best outcome) followed by ICA.

Related

Notch filtering minimizing artifacts in signal

I have to notch filter the following signal.
As you can see it is a signal with really high amplitude.And this causes me a lot of problems. I've designed a bandstop filter (46-55 Hz).The MATLAB code is
Fs=5000; sampling frequency
Wp_NOTCH = [36/(Fs/2) 71/(Fs/2)];
Ws_NOTCH= [46/(Fs/2) 55/(Fs/2)];
Rp_db=-20*log10(.95);
Rs_db=-20*log10(.05);
[order_NOTCH,wn_NOTCH] = buttord(Wp_NOTCH,Ws_NOTCH,Rp_db,Rs_db);
[B_NOTCH,A_NOTCH]=butter(order_NOTCH,wn_NOTCH,'stop');
And the filter is
When i filter the signal (using filtfilt) I've the following signal
As you can see it has a lot of artifacts,and "waves". Is there a way to minimize these artifacts?How can I delete the 50Hz component minimizing these artifacts?Any suggestion?
Thanks in advance
Notch filters are designed to remove sine-waves with a set frequency, they cannot work for a sharp change in the data. The example you posted does not look like a 50Hz artifact, so notch filters cannot help here. You can look-up information on how to deal with data discontinuities, jump or step artifacts, or transient response. Here is some explanation in matlab environment

Matlab:How to LOWER signal to noise of audio

I have some code that block processes the xcorr command with parallel computing too. I want to test how robust cross correlation is with data that has lower S/N.
The only problem is I don't quite know how to apply this. I have an audio file with animal calls. It is mostly noise with intermittent calls. all the calls have different S/N. If you look at the Short Term Fourier Transform spectrogram of the animal call, you will see a blob with a distribution of different intensities.
I want to lower the S/N of these calls so that the shape of the distribution of intensities is still the same, it's just that they are smaller in intensity.
To successfully lower the S/N of the audio, I'm assuming I don't want to change the background noise values either, so only the calls themselves are lowering in amplitude but not the noise.
I don't quite know how I can program this, can I get some help please?
I think you can safely just add extra noise to the whole sample, this is common when testing robustness: if your original sample has say 50 units** of signal and 50 units of noise (SNR of 50/50) then adding say 100 units of noise will lead to a SNR or 50/150, which is lower.
**subject to discussion of what constitutes a unit - power vs voltage, dBs or whatever, it's just for the sake of an example.
If it's important what type of noise you add then perhaps you could copy some from the sections without samples in - doing this manually is usually efficient enough when creating test cases. Alternatively rand will give you random numbers and rng will let you control the distribution.
An alternative method might be to apply a compression function to the spectrograph and then apply the inverse transform to bring it back to the time domain. Beware of using modified versions of your training data to test the algorithm, if you have sufficient data you'd be best to reserve some for testing.
To do this with a sound file editor (plus a file full of noise of your desired distribution and type), first turn down the level of the original file until the signal is at your desired lower level, then mix in the noise file to bring the noise back up to the original level (by setting the appropriate mixer parameters). The resulting mix will have a lower S/N.

Matlab Image Histogram Analysis: how do I test for an underlying bimodal distribution?

I am working with image processing in MATLAB. I have two different images whose histogram plots are as shown below.
Image 1:
and
Image 2:
I have multiple images like those and the only distinguishing(separating) features is that some have single peak and others have two peaks.
In other words some can be thresholded (to generate good results) while others cannot. Is there any way I can separate the two images? Are there any functions that do so in MATLAB or any reference code that will help?
The function used is imhist()
If you mean "distinguish" by "separate", then yes: The property you describe is called bimodality, i.e. you have 2 peaks that can be seperated by one threshold. So your question is actually "how do I test for an underlying bimodal distribution?"
One option to do this programmatically is Binning. This is not the most robust method but the easiest. It might work, it might not.
Kernel Smoothing is probably the more robust solution. You basically shift and scale a certain function (e.g. Gaussian) to fit the data. This can be done with histfit in matlab.
There's more solutions for this problem which you can research for yourself since you now know the terms needed. Be aware though that your problem is not a trivial one if you want to do it properly.

Notch or Bandstop filter and preparing data for it

I am new to matlab and signal processing methods, but i am trying to use its filter properties over a set of data I have. I have a collection of amplitude values obtained at different timestamps. When this is plotted, I get a waveform with several peaks that I can identify. I then perform calculations to derive the time between each consecutive peak and I want to eliminate the rates that are around the range of 48-52peaks per second.
What would be the correct way to go about processing this data step by step? Would a bandstop or notch filter be better if I want to eliminate those frequencies and not attenuate it simply? I am completely lost in the parameters required to feed into the filters for this. Please help...
periodogram is OK, but I would suggest using pwelch instead. It makes a more reasonable PSD estimate and the default parameters are well thought out (Hann windows, 50% overlap of segments, etc.)
If what you want is to remove signals in a wide band (e.g. 48-52 Hz) equally, rather than a single and unchanging frequency, than a bandstop filter is ideal. For example:
fs = 2048;
y = rand(fs*8, 1);
[b,a] = ellip(4, 2, 40, [46 54]/(fs/2));
yy = filter(b,a,y);
This will use a 4th order elliptic bandstop filter to filter the random data variable 'y'. filtfilt.m is also a nice function; it applies the filter forwards and backwards so you get twice the filter action and none of the phase lag or dispersion.
I am currently doing something similar to what you are doing.
I am processing a lot of signals from the Inertial Measurement Unit and motor drives. They all are asynchronously obtained, i.e. they all have very different timestamp and also very different acquisition frequency.
First thing I did was to interpolate all signals data in order to have all signals with same timestamp. You can use the matlab function interp to do this.
After this, you will have all signals with same sample frequency and also timestamp, which will be good in further analysis.
Ok, another thing you can do to analyse the frequency of the peaks is to perform the fourier transform. For beginners i advice the use of periodogram function and not the fft function.
Imagine you signal is x and your sample frequency (after interpolation) is Fs.
You can now use the function periodogram available in matlab like this:
[P,f] = periodogram(x,[],[length(t)],Fs);
This will give the power vs frequency of your signal. After that you will be able to plot and take a look at the frequencies of your signal. In other words, you be able to see the frequencies of the signals that make your acquired signal.
Plot the data this way:
plot(f,P); or semilogy(f,P);
The second is the same thing as the first, but with a logarithmic scale.
After this analysis you can use the Filter Desing and Analysis Tool to design you filter. Just type fdatool in matlab and it will open the design window. Choose the filter type, the cut and pass frequencies and click in design. This tool is very intuitive.
After designing you can export the filter to workspace.
Finally you can use the filter you designed in your signal to see if its what you wanted.
Use the functions filter os filtfilt for this.
Search in the web of the matlab help for the functions I wrote to get more details.
There are a lot of examples availables too.
I hope I could help you.
Good luck.

Notch filters and harmonic noise in matlab

So basically, my problem is that I have a speech signal in .wav format that is corrupted by a harmonic noise source at some frequency. My goal is to identify the frequency at which this noise occurs, and use a notch filter to remove said noise. So far, I have read the speech signal into matlab using:
[data, Fs] = wavread('signal.wav');
My question is how can I identify the frequency at which the harmonic noise is occurring, and once I've done that, how can I go about implementing a notch filter at that frequency?
NOTE: I do not have access to the iirnotch() command or fdesign.notch() due to the version of MATLAB I am currently using (2010).
The general procedure would be to analyse the spectrum, to identify the frequency in question, then design a filter around that frequency. For most real applications it's all a bit woolly: the frequencies move around and there's no easy way to distinguish noise from signal, so you have to use clever techniques and a bit of guesswork. However if you know you have a monotonic corruption then, yes, an FFT and a notch filter will probably do the trick.
You can analyse the signal with fft and design a filter with, among others, fir1, which I believe is part of the signal processing toolbox. If you don't have the signal processing toolbox you can do it 'by hand', as in transform to the frequency domain, remove the frequency(ies) you don't want (by zeroing the relevant elements of the frequency vector) and transform back to time domain. There's a tutorial on exactly that here.
The fft and fir1 functions are well documented: search the Mathworks site to get code examples to get you up and running.
To add to/ammend xenoclast's answer, filtering in the frequency domain may or may not work for you. There are many thorny issues with filtering in the frequency domain, some of which are covered here: http://blog.bjornroche.com/2012/08/why-eq-is-done-in-time-domain.html
One additional issue is that if you try to process your entire file at once, the "width" or Q of the filters will depend on the length of your file. This might work out for you, or it might not. If you have many files of different lengths, don't expect similar results this way.
To design your own IIR notch filter, you could use the RBJ audio filter cookbook. If you need help, I wrote up a tutorial here:
http://blog.bjornroche.com/2012/08/basic-audio-eqs.html
My tutorial uses bell/peaking filter, but it's easy to follow that and then replace it with a notch filter from RBJ.
One final note: assuming this is actually an audio signal in your .wav file, you can also use your ears to find and fix the problem frequencies:
Open the file in an audio editing program that lets you adjust filter settings in real-time (I am not sure if audacity lets you do this, but probably).
Use a "boost" or "parametric" filter set to a high gain and sweep the frequency setting until you hear the noise accentuated the most.
replace the boost filter with a notch filter of the same frequency. You may need to tweak the width to trade off noise elimination vs. signal preservation.
repete as needed (due to the many harmonics).
save the resulting file.
Of course, some audio editing apps have built-in harmonic noise reduction features that work especially well for 50/60 Hz noise.