I have a signal consisting of fast oscillating AC part and slowly varying DC part. I need to calcuate its DC part (and envelope, but that's not important now).
I could use the STFT, filter and transform it back, but it's a little inefficient cause I am not looking for a whole spectrum. Any other ideas?
I have read the articles on MathWorks and my maths is good enough to design something general and complicated, but I am looking for tips, hints or smart and elegant simple solutions. Thanks in advance!
DC isolation is basically low-pass filtering. You want to remove all the high-frequency components, while preserving the low. There are tons of approaches to this problem, but it looks like you're looking for something quick and simple.
One of the simplest forms of low-pass filter is the moving average. It's messy in the frequency domain, but for many simple applications it is good enough, trivial to implement, and incredibly fast.
For reasons I'll get to later on, it isn't actually a very good low-pass filter when you consider the frequency domain, but it is a very good smoothing filter in the time domain, which is what it sounds like you need.
Update: After posting this, I realized this was asked with the MATLAB tag, not just signal-processing. I've added a bit at the end showing the simplest way to do it in MATLAB.
Moving Average Filter
Parameters:
N: Filter order, number of samples to average
Input:
xt: Input at time t. For negative t assume x is zero.
Output:
yt: Output at time t. This is the average of the previous N samples.
Algorithm
Initialization (t = 0)
y0 = x0 / N
At each time step (t > 0)
yt = *yt-1 + (xt - xt-N ) / N
Filter Frequency Response
The impulse response of the filter is a rectangular pulse the width of the filter order. This gives a straightforward frequency response:
H[f] = | sin(πfN) / (Nsin(πf)) |
This shows how horrible the frequency response is, but for many smoothing applications it doesn't really matter. The key thing to notice is that the first zero happens at f = 1/N. This gives you a good idea of what order filter to use in order to smooth out components above a specific frequency, as described in the next section.
Parameter Selection
Given a sampling rate fs and a cut-off frequency fc, the normalized cutoff frequency is simply f = fc / fs. This, plus the frequency response above, gives a simple way to come up with the filter order, although tweaking it slightly by hand may improve results:
N = 1/f = fs / fc
Another way to read this formula is that it is important that the filter order is smaller than the fastest DC feature that you wish to preserve. Otherwise it will be partially filtered out.
Time-Domain Response
To illustrate the time-domain response, I'll work through some examples. I'll be using a sample rate of fs = 1000.
I generated a 40ms rectangular pulse with noise added to demonstrate the effect of increasing the filter order.
As you can see, increasing the filter order smooths out the noise, but it has other effects as well. Higher orders mean a longer delay, and a slower response to step changes. Once the order gets too high, the results are worhtless. The N = 51 example shows what happens when the filter order is wider than the event that you want to capture.
Although those sloping edges may look bad, the moving average filter actually gives the steepest possible edges for a given amount of noise reduction that is possible for a linear filter of the same order, and due to the incredibly simple algorithm it is also the fastest linear filter to implement. Because it has only one parameter, it is also simple to tweak to find the optimal order for your application.
Other Tweaks and Implementation Tips
Multipass Filter
The simplest way to improve the moving average filter's response in the frequency domain is to run multiple passes. After a few runs it becomes a very good approximation of a Gaussian filter due ot the central limit theorem. This slows down the step response a bit, but it also rounds it off. Higher frequencies will be attenuated further, although it also affects the roll-off in the low-frequency end. The performance hit for adding a few more passes is minimal.
Circular Buffer
If you are processing a stream of data, then you don't always have an easy way to lookup xt-N. By using a circular buffer to keep track of the last N samples you end up with a simple way to look up the sample you need with only a few steps added to the algorithm - it preserves the constant-time per step regardless of filter order.
Roundoff Error
If you're working with fixed-point or integer values the roundoff can be completely eliminated. Simply don't divide by N in the filter algorithm:
at = *at-1 + xt - xt-N
yt = *at / N
However, you need to take care to make sure that this won't cause an overflow in a.
With floating-point values, the tiny rounding errors could add up over time. The simplest solution for a long-running filter is to calculate y by averaging the entire buffer of the last N samples every so often. This will reset you to a point where there is no accumulated error, while letting you use the fast algorithm for the vast majority of time steps.
Doing it in MATLAB
A moving average is a linear FIR filter with each weight set the same. This means the numerators are all the same and sum to one, and the denominator is 1. It's simple to use the filter function to perform a moving average:
y = filter(ones(1,N)/N, 1, x);
However, that isn't going to use the fast algorithm discussed above. If performance is an issue then you'll want to implement the algorithm yourself.
Related
I have audio record.
I want to detect sinusoidal pattern.
If i do regular fft i have result with bad SNR.
for example
my signal contents 4 high frequencies:
fft result:
To reduce noise i want to do Coherent integration as described in this article: http://flylib.com/books/en/2.729.1.109/1/
but i cant find any MATLAB examples how to do it. Sorry for bad english. Please help )
I look at spectra almost every day, but I never heard of 'coherent integration' as a method to calculate one. As also mentioned by Jason, coherent integration would only work when your signal has a fixed phase during every FFT you average over.
It is more likely that you want to do what the article calls 'incoherent integration'. This is more commonly known as calculating a periodogram (or Welch's method, a slightly better variant), in which you average the squared absolute value of the individual FFTs to obtain a power-spectral-density. To calculate a PSD in the correct way, you need to pay attention to some details, like applying a suitable Fourier window before doing each FFT, doing the proper normalization (so that the result is properly calibrated in i.e. Volt^2/Hz) and using half-overlapping windows to make use of all your data. All of this is implemented in Matlab's pwelch function, which is part of the signal-processing toolbox. See my answer to a similar question about how to use pwelch.
Integration or averaging of FFT frames just amounts to adding the frames up element-wise and dividing by the number of frames. Since MATLAB provides vector operations, you can just add the frames with the + operator.
coh_avg = (frame1 + frame2 + ...) / Nframes
Where frameX are the complex FFT output frames.
If you want to do non-coherent averaging, you just need to take the magnitude of the complex elements before adding the frames together.
noncoh_avg = (abs(frame1) + abs(frame2) + ...) / Nframes
Also note that in order for coherent averaging to work the best, the starting phase of the signal of interest needs to be the same for each FFT frame. Otherwise, the FFT bin with the signal may add in such a way that the amplitudes cancel out. This is usually a tough requirement to ensure without some knowledge of the signal or some external triggering so it is more common to use non-coherent averaging.
Non-coherent integration will not reduce the noise power, but it will increase signal to noise ratio (how the signal power compares to the noise power), which is probably what you really want anyway.
I think what you are looking for is the "spectrogram" function in Matlab, which computes the short time Fourier transform(STFT) of an input signal.
STFT
Spectrogram
First for those, who are not familiar with Simulink, there is a imaginable outside-Simulink partial solution:
I need to create a vector satisfying the following conditions:
known initial value a1
known final value a2
it has a pre-defined step size, but the length is not pre-determined
the first derivative over the whole range is limited to v_max resp. -v_max
the second derivative over the whole range is limited to a_max resp. -a_max
the third derivative over the whole range is limited to j_max resp. -j_max
at the first and the final point all derivatives are zero.
Before you ask "what have you tried so far", I just had the idea to solve it outside Simulink and I tried the whole stuff below ;)
But maybe you guys have a good idea, while I keep working on my own solution.
I'd like to generate smooth ramp signals (3rd derivative limited) based on a trigger signal in Simulink.
To get a triggered step I created a triggered subsystem propagating the trigger output. It looks like that:
But I actually don't want a step, I need a very smooth ramp with limited derivatives up to the 3rd order. The math behind is:
displacement: x
speed: v = x'
acceleration: a = v' = x''
jerk: j = a' = v'' = x'''
(If this looks familiar to you, I once had a very similar question. I thought about a bounty on it, but after the necessary edit of the question both answers would have been invalid)
As there are just rate limiters of 1st order, I used two derivates and a double integration to resolve my problem. But there is a mayor drawback, I can not ignore anymore. For the sake of illustration I chose a relatively big step size of 0.1.
The complete minimal example (Fixed Step, stepsize: 0.1, ode4): Download here
It can be seen, that the signal not even reaches the intended step height of 10 and furthermore is not constant at the end.
Over the development process of my whole model, this approach was satisfactory enough for small step sizes. But I reached the point where I really need the smooth ramp as intended. That means I need a finally constant signal at exactly the value, specified by the step height gain.
I already spent days to resolve the problem, and hope to fine some help here now.
Some of my ideas:
dynamically increase the step height over the actual desired value and saturate the final output. If the rate limits,step height and the simulation step size wouldn't be flexible one could probably find a satisfying solution. But as everything has to be flexible, there are too much cases where the acceleration and jerk limit is violated.
I tried to use the Matlab function block and write my own 3rd order rate limiter. Though it seems possible for me for the trigger moment, I have no solution how to smooth the "deceleration" at the end of the ramp. Also I'd need C-compilers, which would make it hard to use my model on other systems without problems. (At least I think so.)
The solver can not be changed siginificantly (either ode3 or ode4) and a fixed step size is mandatory (0.00001 to 0.01).
Currently used, not really useful approach:
For a dynamic amplification of 1.07 I get the following output (all values normalised on their limits):
Though the displacement looks nice, the violation of the acceleration limit is very harmful.
For a dynamic amplification of 1.05 I get the following output (all values normalised on their limits):
The acceleration stays in its boundaries, but the displacement does not reach the intended value. (not really clear in the picture) The jerk is still to big. (I could live with that, but it's not nice)
So it appears to me that a inside-Simulink solutions is far from reality. Any ideas how to create a well-behaving custom function block?
Simulation step size, step height, and the rate limits are known before the simulation starts. (But I have a lot of these triggered smooth ramps in a row, it should feed a event-discrete control). So I could imagine to create the whole smooth ramp outside simulink and save it as a timeseries object and append it on the current signal when the trigger is activated.
The problems you see are because the difference is not conditioned very well.
Taking the difference amplifies the numerical that exists in your simulation.
Also the jerk will always be large if you try to apply an actual step.
I guess for your approach it would be better to work the other way around:
i.e. make a jerk, acceleration and velocity with which your step is achieved.
I think your looking for something like the ref3 block:
http://www.dct.tue.nl/home_of_ref3.htm
Note the disclaimer on the site and that it is a little cumbersome to use.
An easy (yet to be improved) way is to use a rate limiter and then a state space model with a filter. From the filter you get the velocity, which in turn you can apply a rate limiter to. You continue with rate-limiter and filters until you have the desired curve.
Otherwise you can come up with numerical rate-limiters of higher order using ie runge kutta formulas or finite differences. However it was pointed out, that they may suffer from bad conditioning.
What I usually do is to use one rate limiter and a filter of 3rd Order and just tune the time constant (1 tripple pole), such that my needs are met. This works well, esp
Integrator chains of length > 1 are unstable!
There is a huge field of research dealing with trajectory planning. The easiest way might be to use FIR filters (Biagotti et al) or to implement an online trajectory planner (Ezair et al 2014 / Knierim et al 2012).
i'm trying to use Berchin's FDLS methods (for those that don't know what it is, it's a way to design arbitrary magnitude and phase of an IIR Eq)
I tried different things but neither gave me what i asked:
1) i tried to design an allpass filter with arbitrary phase response (nothing fancy just a linear phase and a short deviation for 5 points) and it didn't worked as planned.
2) i tried a linear phase response for a -12 db notch and the same way it didn't worked (in black the desired response)
a http://nsa34.casimages.com/img/2013/08/22/13082211272913316.jpg
i used a samplerate of 316, M=158 points and a filter order of around 50.
Do i need to use more points to increase the filter order or is it just impossible to design my filters using that algorithm?
thanks for you answers
Jeff
With FDLS, the parameters to play with to find alternate solutions, are not only the total filter order, but the ratio of zeros to poles. IIRC, more of both isn't always better (possibly for numerical reasons). So try smaller numbers of poles and zeros (only a few of each), increase the numbers separately, and see if you get a more appropriate match to your desired frequency response.
In matlab we can use filtfilt function to filter out data which implements forward and backward filtering techniques which results in zero-phase. But it's difficult to implement this filter in real time as it involves backward filtering.
I want to implement a 1st order high-pass or low pass filter with zero phase in real time. How can i achieve this?
I have search the web for days but unable to get any clue to start with it!
Thanks in advance!
It is not possible to perform a zero-phase filter in real-time because a zero-phase filter requires filter coefficients that are symmetric around zero. That means that the filter is non-causal, or that current output depends on future input. This of course is not possible in the real-time case and can be faked as in the case of filtfilt during post-processing.
What you probably are looking for is a linear phase filter. Don't let the name confuse you; this does not mean that the filter produces any phase distortion. It only means that a time shift is applied to the output. A linear phase shift with respect to frequency results in a constant shift with respect to time. So basically your output will be delayed some constant number of samples (group delay) from the input.
So the only difference between a zero-phase and linear-phase filter is that the linear-phase filter output is a delayed version of the zero-phase output. This delay can be accounted for by keeping track of the group delay if you need to keep the output aligned in time with the input.
Response to comment:
FIR filters are guaranteed to be linear phase if their coefficients are symmetric about the center. MATLAB can easily create these types of filters with functions such as fir1 or firpm. The examples in the documentation of those functions should show you how to use them.
The group delay of a linear phase FIR filter is (L-1)/2 where L is the length of the filter. Because of this and a few other things, I would usually choose an odd filter length so the delay is aligned to a sample and not in between samples. This basically means that the output signal will be delayed from the input by (L-1)/2 samples.
Implementing the actual filtering process is basically discrete convolution of the input with the filter. This involves reversing the filter coefficients, multiplying them by the most recent L input samples, and adding those results to produce a single output sample. Then a new input sample is brought in and the whole process is done again to produce another sample (mutiplying and summing over a sliding window). You should be able to find some sample code for convolution on the web.
This is the direct way to perform FIR filtering, but for longer filters, it may be more efficient to perform fast convolution with an FFT. This will be a lot more difficult to get right, so unless you are talking about high sample rates and long filters, I would just go with the direct approach.
A "non-causal" zero-phase filter plus a sufficient amount of added delay can be approximated by a causal linear phase FIR filter. This assumes that adding some delay fits your system requirements.
In your case, you could take the impulse response of your forward+backward asymmetric (or non-linear phase) filtering process, window that impulse response to make the response finite in length, delay the finite length kernel so that it doesn't require "future" samples, and use that as a FIR filter kernel. You will have to check the results to see if your chosen window was appropriate in length and shape. There may be a trade-off in choosing a delay versus quality of the filter due to the finite length windowing required for that delay.
Does anyone know how to use filters in MATLAB?
I am not an aficionado, so I'm not concerned with roll-off characteristics etc — I have a 1 dimensional signal vector x sampled at 100 kHz, and I want to perform a high pass filtering on it (say, rejecting anything below 10Hz) to remove the baseline drift.
There are Butterworth, Elliptical, and Chebychev filters described in the help, but no simple explanation as to how to implement.
There are several filters that can be used, and the actual choice of the filter will depend on what you're trying to achieve. Since you mentioned Butterworth, Chebyschev and Elliptical filters, I'm assuming you're looking for IIR filters in general.
Wikipedia is a good place to start reading up on the different filters and what they do. For example, Butterworth is maximally flat in the passband and the response rolls off in the stop band. In Chebyschev, you have a smooth response in either the passband (type 2) or the stop band (type 1) and larger, irregular ripples in the other and lastly, in Elliptical filters, there's ripples in both the bands. The following image is taken from wikipedia.
So in all three cases, you have to trade something for something else. In Butterworth, you get no ripples, but the frequency response roll off is slower. In the above figure, it takes from 0.4 to about 0.55 to get to half power. In Chebyschev, you get steeper roll off, but you have to allow for irregular and larger ripples in one of the bands, and in Elliptical, you get near-instant cut off, but you have ripples in both bands.
The choice of filter will depend entirely on your application. Are you trying to get a clean signal with little to no losses? Then you need something that gives you a smooth response in the passband (Butterworth/Cheby2). Are you trying to kill frequencies in the stopband, and you won't mind a minor loss in the response in the passband? Then you will need something that's smooth in the stop band (Cheby1). Do you need extremely sharp cut-off corners, i.e., anything a little beyond the passband is detrimental to your analysis? If so, you should use Elliptical filters.
The thing to remember about IIR filters is that they've got poles. Unlike FIR filters where you can increase the order of the filter with the only ramification being the filter delay, increasing the order of IIR filters will make the filter unstable. By unstable, I mean you will have poles that lie outside the unit circle. To see why this is so, you can read the wiki articles on IIR filters, especially the part on stability.
To further illustrate my point, consider the following band pass filter.
fpass=[0.05 0.2];%# passband
fstop=[0.045 0.205]; %# frequency where it rolls off to half power
Rpass=1;%# max permissible ripples in stopband (dB)
Astop=40;%# min 40dB attenuation
n=cheb2ord(fpass,fstop,Rpass,Astop);%# calculate minimum filter order to achieve these design requirements
[b,a]=cheby2(n,Astop,fstop);
Now if you look at the zero-pole diagram using zplane(b,a), you'll see that there are several poles (x) lying outside the unit circle, which makes this approach unstable.
and this is evident from the fact that the frequency response is all haywire. Use freqz(b,a) to get the following
To get a more stable filter with your exact design requirements, you'll need to use second order filters using the z-p-k method instead of b-a, in MATLAB. Here's how for the same filter as above:
[z,p,k]=cheby2(n,Astop,fstop);
[s,g]=zp2sos(z,p,k);%# create second order sections
Hd=dfilt.df2sos(s,g);%# create a dfilt object.
Now if you look at the characteristics of this filter, you'll see that all the poles lie inside the unit circle (hence stable) and matches the design requirements
The approach is similar for butter and ellip, with equivalent buttord and ellipord. The MATLAB documentation also has good examples on designing filters. You can build upon these examples and mine to design a filter according to what you want.
To use the filter on your data, you can either do filter(b,a,data) or filter(Hd,data) depending on what filter you eventually use. If you want zero phase distortion, use filtfilt. However, this does not accept dfilt objects. So to zero-phase filter with Hd, use the filtfilthd file available on the Mathworks file exchange site
EDIT
This is in response to #DarenW's comment. Smoothing and filtering are two different operations, and although they're similar in some regards (moving average is a low pass filter), you can't simply substitute one for the other unless it you can be sure that it won't be of concern in the specific application.
For example, implementing Daren's suggestion on a linear chirp signal from 0-25kHz, sampled at 100kHz, this the frequency spectrum after smoothing with a Gaussian filter
Sure, the drift close to 10Hz is almost nil. However, the operation has completely changed the nature of the frequency components in the original signal. This discrepancy comes about because they completely ignored the roll-off of the smoothing operation (see red line), and assumed that it would be flat zero. If that were true, then the subtraction would've worked. But alas, that is not the case, which is why an entire field on designing filters exists.
Create your filter - for example using [B,A] = butter(N,Wn,'high') where N is the order of the filter - if you are unsure what this is, just set it to 10. Wn is the cutoff frequency normalized between 0 and 1, with 1 corresponding to half the sample rate of the signal. If your sample rate is fs, and you want a cutoff frequency of 10 Hz, you need to set Wn = (10/(fs/2)).
You can then apply the filter by using Y = filter(B,A,X) where X is your signal. You can also look into the filtfilt function.
A cheapo way to do this kind of filtering that doesn't involve straining brain cells on design, zeros and poles and ripple and all that, is:
* Make a copy of the signal
* Smooth it. For a 100KHz signal and wanting to eliminate about 10Hz on down, you'll need to smooth over about 10,000 points. Use a Gaussian smoother, or a box smoother maybe 1/2 that width twice, or whatever is handy. (A simple box smoother of total width 10,000 used once may produce unwanted edge effects)
* Subtract the smoothed version from the original. Baseline drift will be gone.
If the original signal is spikey, you may want to use a short median filter before the big smoother.
This generalizes easily to 2D images, 3D volume data, whatever.