Instrument Neutral Distributed Interface INDI  2.0.2
transforms.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright(c) 2017 Jasem Mutlaq. All rights reserved.
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 *******************************************************************************/
18 
19 #include "transforms.h"
20 #include "indistandardproperty.h"
21 #include "indicom.h"
22 #include "indilogger.h"
23 #include "dsp.h"
24 
25 #include <dirent.h>
26 #include <cerrno>
27 #include <cstring>
28 #include <algorithm>
29 #include <thread>
30 #include <chrono>
31 
32 namespace DSP
33 {
34 
36 {
37 }
38 
40 {
41 }
42 
43 bool FourierTransform::processBLOB(uint8_t *buf, uint32_t dims, int *sizes, int bits_per_sample)
44 {
45  if(!PluginActive) return false;
46  setStream(buf, dims, sizes, bits_per_sample);
47 
50 }
51 
53 {
54  IUFillBLOB(&DownloadB, "PHASE_DOWNLOAD", "Phase", "");
55  IUFillBLOBVector(&DownloadBP, &FitsB, 1, m_Device->getDeviceName(), "PHASE", "Phase Data", DSP_TAB, IP_RW, 60, IPS_IDLE);
56 }
57 
59 {
60 }
61 
63 {
64  m_Device->defineProperty(&DownloadBP);
66 }
67 
69 {
70  m_Device->deleteProperty(DownloadBP.name);
72 }
73 
74 bool InverseFourierTransform::processBLOB(uint8_t *buf, uint32_t dims, int *sizes, int bits_per_sample)
75 {
76  if(!PluginActive) return false;
77  if(!phase_loaded) return false;
78  setStream(buf, dims, sizes, bits_per_sample);
79  if (phase->dims != stream->dims) return false;
80  for (int d = 0; d < stream->dims; d++)
81  if (phase->sizes[d] != stream->sizes[d])
82  return false;
83  setMagnitude(buf, dims, sizes, bits_per_sample);
84  stream->phase = phase;
87  return Interface::processBLOB(getStream(), stream->dims, stream->sizes, bits_per_sample);
88 }
89 
90 bool InverseFourierTransform::ISNewBLOB(const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[],
91  char *formats[], char *names[], int n)
92 {
93  if(!strcmp(dev, getDeviceName()))
94  {
95  if(!strcmp(name, DownloadBP.name))
96  {
97  IUUpdateBLOB(&DownloadBP, sizes, blobsizes, blobs, formats, names, n);
98  LOGF_INFO("Received phase BLOB for %s", getDeviceName());
99  if(phase != nullptr)
100  {
101  dsp_stream_free_buffer(phase);
102  dsp_stream_free(phase);
103  }
104  phase = loadFITS(blobs[0], sizes[0]);
105  if (phase != nullptr)
106  {
107  LOGF_INFO("Phase for %s loaded", getDeviceName());
108  phase_loaded = true;
109  return true;
110  }
111  }
112  }
113  return false;
114 }
115 
116 Spectrum::Spectrum(INDI::DefaultDevice *dev) : Interface(dev, DSP_SPECTRUM, "SPECTRUM", "Spectrum")
117 {
118 }
119 
121 {
122 }
123 
124 bool Spectrum::processBLOB(uint8_t *buf, uint32_t dims, int *sizes, int bits_per_sample)
125 {
126  if(!PluginActive) return false;
127  setStream(buf, dims, sizes, bits_per_sample);
128 
130  double *histo = dsp_stats_histogram(stream->magnitude, 4096);
131  return Interface::processBLOB(static_cast<uint8_t*>(static_cast<void*>(histo)), 1, new int{4096}, -64);
132 }
133 
134 
135 Histogram::Histogram(INDI::DefaultDevice *dev) : Interface(dev, DSP_HISTOGRAM, "HISTOGRAM", "Histogram")
136 {
137 }
138 
140 {
141 }
142 
143 bool Histogram::processBLOB(uint8_t *buf, uint32_t dims, int *sizes, int bits_per_sample)
144 {
145  if(!PluginActive) return false;
146  setStream(buf, dims, sizes, bits_per_sample);
147 
148  double *histo = dsp_stats_histogram(stream, 4096);
149  return Interface::processBLOB(static_cast<uint8_t*>(static_cast<void*>(histo)), 1, new int{4096}, -64);
150 }
151 }
FourierTransform(INDI::DefaultDevice *dev)
Definition: transforms.cpp:35
virtual bool processBLOB(uint8_t *out, uint32_t dims, int *sizes, int bits_per_sample) override
processBLOB Propagate to Callback and generate BLOBs for parent device.
Definition: transforms.cpp:43
virtual bool processBLOB(uint8_t *out, uint32_t dims, int *sizes, int bits_per_sample) override
processBLOB Propagate to Callback and generate BLOBs for parent device.
Definition: transforms.cpp:143
Histogram(INDI::DefaultDevice *dev)
Definition: transforms.cpp:135
const char * getDeviceName()
uint8_t * getStream()
virtual void Activated()
Activated Called after activation from client application.
uint8_t * getMagnitude()
virtual bool processBLOB(uint8_t *buf, uint32_t ndims, int *dims, int bits_per_sample)
processBLOB Propagate to Callback and generate BLOBs for parent device.
dsp_stream_p stream
Definition: dspinterface.h:213
bool setStream(void *buf, uint32_t dims, int *sizes, int bits_per_sample)
INDI::DefaultDevice * m_Device
Definition: dspinterface.h:198
dsp_stream_p loadFITS(char *buf, int len)
loadFITS Converts FITS data into a dsp_stream structure pointer.
bool setMagnitude(void *buf, uint32_t dims, int *sizes, int bits_per_sample)
virtual void Deactivated()
Deactivated Called after deactivation from client application.
bool ISNewBLOB(const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[], char *formats[], char *names[], int n) override
Definition: transforms.cpp:90
void Deactivated() override
Deactivated Called after deactivation from client application.
Definition: transforms.cpp:68
virtual bool processBLOB(uint8_t *out, uint32_t dims, int *sizes, int bits_per_sample) override
processBLOB Propagate to Callback and generate BLOBs for parent device.
Definition: transforms.cpp:74
InverseFourierTransform(INDI::DefaultDevice *dev)
Definition: transforms.cpp:52
void Activated() override
Activated Called after activation from client application.
Definition: transforms.cpp:62
virtual bool processBLOB(uint8_t *out, uint32_t dims, int *sizes, int bits_per_sample) override
processBLOB Propagate to Callback and generate BLOBs for parent device.
Definition: transforms.cpp:124
Spectrum(INDI::DefaultDevice *dev)
Definition: transforms.cpp:116
const char * getDeviceName() const
Definition: basedevice.cpp:821
Class to provide extended functionality for devices in addition to the functionality provided by INDI...
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
void defineProperty(INumberVectorProperty *property)
struct dsp_stream_t * phase
Fourier transform phase.
Definition: dsp.h:413
int * sizes
Sizes of each dimension.
Definition: dsp.h:373
struct dsp_stream_t * magnitude
Fourier transform magnitude.
Definition: dsp.h:411
int dims
Number of dimensions of the buffers.
Definition: dsp.h:371
dsp_t * buf
buffer
Definition: dsp.h:375
int len
The buffers length.
Definition: dsp.h:369
#define dsp_buffer_set(buf, len, _val)
Fill the buffer with the passed value.
Definition: dsp.h:815
DLL_EXPORT void dsp_stream_free(dsp_stream_p stream)
Free the DSP stream passed as argument.
Definition: stream.c:163
DLL_EXPORT void dsp_stream_free_buffer(dsp_stream_p stream)
Free the buffer of the DSP Stream passed as argument.
Definition: stream.c:112
DLL_EXPORT void dsp_fourier_idft(dsp_stream_p stream)
Perform an inverse discrete Fourier Transform of a dsp_stream.
Definition: fft.c:172
DLL_EXPORT void dsp_fourier_dft(dsp_stream_p stream, int exp)
Perform a discrete Fourier Transform of a dsp_stream.
Definition: fft.c:135
DLL_EXPORT double * dsp_stats_histogram(dsp_stream_p stream, int size)
Histogram of the inut stream.
Definition: stats.c:22
@ IP_RW
Definition: indiapi.h:186
@ IPS_IDLE
Definition: indiapi.h:161
Implementations for common driver routines.
void IUFillBLOBVector(IBLOBVectorProperty *bvp, IBLOB *bp, int nbp, const char *dev, const char *name, const char *label, const char *group, IPerm p, double timeout, IPState s)
Assign attributes for a BLOB vector property. The vector's auxiliary elements will be set to NULL.
Definition: indidevapi.c:310
void IUFillBLOB(IBLOB *bp, const char *name, const char *label, const char *format)
Assign attributes for a BLOB property. The BLOB's data and auxiliary elements will be set to NULL.
Definition: indidevapi.c:216
int IUUpdateBLOB(IBLOBVectorProperty *bvp, int sizes[], int blobsizes[], char *blobs[], char *formats[], char *names[], int n)
Update all BLOB members in a BLOB vector property.
Definition: indidriver.c:1422
#define LOGF_INFO(fmt,...)
Definition: indilogger.h:82
The DSP Namespace adds signal processing to INDI drivers. Primarily written for sensors and detectors...
Definition: convolution.cpp:40
const char * DSP_TAB
char name[MAXINDINAME]
Definition: indiapi.h:475