Remove High-Frequency Noise in Measured Data
This example shows how to design a low-pass filter and use it to remove high-frequency noise in measured data. High-frequency noise is due to components of a signal varying faster than the signal of interest. Removing high-frequency noise allows the signal of interest to be more compactly represented and enables more accurate analysis. A low-pass filter is a common techqnique for removing high-frequency noise in a signal.
The ThingSpeak™ channel 12397 contains data from the MathWorks® weather station, located in Natick, Massachusetts. The data is collected and posted to ThingSpeak once per minute. Field 3 of the channel contains relative humidity data. Read the data using the
[humidity,time] = thingSpeakRead(12397,'NumPoints',8000,'Fields',3);
A filter is a process that removes unwanted components from a signal. A low-pass filter is designed to let lower frequency components pass through and block higher frequency components in a signal. DSP System Toolbox™ provides multiple techniques to define a low-pass filter. This example designs a third-order finite impulse response (FIR) filter. The sampling frequency is once every 60 seconds (Fs=1/60), as the data in channel 12397 is uploaded once per minute. The low-pass filter keeps low frequency components and attenuates high-frequency components with a period of less than 24 hours.
filtertype = 'FIR'; Fs = 1/60; N = 3; Fpass = 1/(24*60*60); Fstop = 1/(2*60*60); Rp = 0.5; Astop = 50; LPF = dsp.LowpassFilter('SampleRate',Fs,... 'FilterType',filtertype,... 'PassbandFrequency',Fpass,... 'StopbandFrequency',Fstop,... 'PassbandRipple',Rp,... 'StopbandAttenuation',Astop);
Process and Send the Data to ThingSpeak
Process the relative humidity data using the low-pass filter, and send the filtered humidity data to a ThingSpeak channel using the
Output = step(LPF, humidity);
Using the MATLAB Analysis app, you can write the data to a channel. If you are using the MATLAB Visualizations app, you can also add a plot of the data. Change the
channelID and the
writeAPIKey to send data to your channel.
channelID = 17504; writeAPIKey='23ZLGOBBU9TWHG2H'; thingSpeakWrite(channelID,Output,'Timestamps',time,'WriteKey',writeAPIKey); plot(time,humidity,time,Output); ylabel('Relative Humidity'); legend('Raw Data', 'Filtered Data');
The plot demonstrates a dramatic reduction in high-frequency noise.
- Compensate for Delay and Distortion Introduced by Filters (Signal Processing Toolbox)
- Filter Design and Analysis (DSP System Toolbox)