As the graph shows, I have slightly over 0.01 milliseconds delay introduced by transfer function for a simple low-pass ASK filter at the demodulation part.
I need to get rid of this delay by any means.
Scope Results
I tried to increase the frequency at the denominator coefficient of the transfer function, but still with the same delay.
In the last attempts, I tried to create a subsystem that outputs binary 1 at interval or 0.5 milliseconds if it is bigger than 0.5e-6 as threshold, and hold the value until the coming 1.5 millisecond where it should outputs 0 if it is less than 0.05e-6 and so on. I tried to follow this here, but it didn't work on my scenario. I also tried this here, but my attempts failed.
Here is an overall implementation for the demodulation part using simulink.
And the following is the transfer function for a simple low-pass ASK filter:
Help here is much appreciated.
It is impossible for a linear filter to filter a signal (for any finite bandwidth above DC) without a delay. It takes some time (usually related to the period of the center frequency of a bandpass filter), for the filter to gather enough information from the signal to differentiate between a waveform to pass and a waveform to attenuate.
You might be able to pass a sharper rise time or fall time by using a matched filter with the expected transient(s) as the template(s), but that would have an even greater delay.
Usually this delay is accounted for by using a matching delay in other parts of the system to synchronize timing as needed.
Related
Review for removing periodicsI have a dataset that contains hourly wind speed data for 7 seven. I am trying to implement a forecasting model to the data and the review paper states that trimming of diurnal, weekly, monthly, and annual patterns in data significantly enhances estimation accuracy. They then follow along by using the fourier series to remove the periodic components as seen in the image. Any ideas on how i model this in matlab?
I am afraid this topic is not explained "urgently". What you need is a filter for the respective frequencies and a certain number of their harmonics. You can implement such a filter with an fft or directly with a IIR/FIR-formula.
FFT is faster than a IIR/FIR-implementation, but requires some care with respect to window function. Even if you do a "continuous" DFT, you will have a window function (like exponential or gaussian). The window function determines the bandwidth. The wider the window, the smaller the bandwidth. With an IIR/FIR-filter the bandwidth is encoded in the recursive parameters.
For suppressing single frequencies (like the 24hr weather signal) you need a notch-filter. This also requires you to specify a bandwidth, as you can see in the linked article. The smaller the bandwidth, the longer it will take (in time) until the filter has evolved to the frequency to suppress it. If you want the filter to recognize the amplitude of the 24hr-signal fast, then you need a wider bandwidth. But then however you are going to suppress also more frequencies slightly lower and slightly higher than 1/24hrs. It's a tradeoff.
If you also want to suppress several harmonics (like described in the paper) you have to combine several notch-filters in series. If you want to do it with FFT, you have to model the desired transfer function in the frequency space and since you can do this for all frequencies at once, so it's more efficient.
An easy but approximate way to get something similar to a notch-filter including all harmonics is with a Comb-filter. But it's an approximation, you have no control over the details of the transfer function. You could do that in Matlab by adding to the original a signal that is shifted by 12hrs. This is because a sinusoidal signal will cancel with one that is shifted by pi.
So you see, there's lots of possibilities for what you want.
I have a quick question on sensor data decimation, which I'm sure is pretty easy but thought I'd check. I have a sensor that is sampling at 25Hz and the data is being sent across a serial RS232 connection to an external data logger, which is logging the data at 10Hz.
I think if I want to recover a true 10Hz signal, I should I pass the original 25 Hz signal through a decimation process (i.e. filtering followed by down sampling). Is this correct?
If it is correct, I was thinking that I should decimate the original 25Hz signal by passing it through a low pass filter with a cutoff frequency of ~10 Hz, to remove the higher frequency components. The filtered signal would then be down sampled to produce a final signal. This down sampling would be achieved by extracting a value every 2.5 samples from the filtered signal.
So in other words, the 1st value of the final signal would be the first sample of the filtered signal. The second value of the final signal would be the average of samples 2 & 3 from the filtered signal. Then the third value of the final signal would be sample 5 from the filtered signal, and the fourth sample would be an average between samples 7 & 8 etc.....
Hopefully that makes sense. I think that would provide me with a clean 10Hz signal.
Many thanks for your time and efforts on this, they are very much appreciated
Cheers
The type of filtering you should use will depend in part on what you are using this signal for and how noisy the captured data is.
In general, you should not change the sampling frequency of the filter constantly, this could introduce artificial periodic noise in the captured data. My guess is that for this process you are sampling something that doesn't change rapidly. You might want to just take a rolling average of the last 3 samples, even though some of the data averaged into each logged sample will overlap.
I have data from an accelerometer and made a graph of acceleration(y-axis) and time (x-axis). The frequency rate of the sensor is arround 100 samples per second. but there is no equally spaced time (for example it goes from 10.046,10.047,10.163 etc) the pace is not const. And there is no function of the signal i get. I need to find the frequency of the signal and made a graph of frequency(Hz x-axis) and acceleration (y-axis). but i don't know which code of FFT suits my case. (sorry for bad english)
Any help would be greatly appreciated
For an FFT to work you will need to reconstruct the signal you have with with a regular interval. There are two ways you can do this:
Interpolate the data you already have to make an accurate guess at where the signal would be at a regular interval. However, this FFT may contain significant inaccuracies.
OR
Adjust the device reading from the accelerometer incorporate an accurate timer such that results are always transmitted at regular intervals. This is what I would recommend.
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.
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.