2023-09-21 11:43:10 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2023 Kyle Reed
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __IQ_TRIM_H__
|
|
|
|
#define __IQ_TRIM_H__
|
|
|
|
|
|
|
|
#include "complex.hpp"
|
|
|
|
#include "file.hpp"
|
|
|
|
#include "optional.hpp"
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <limits>
|
|
|
|
|
2023-09-23 15:56:37 -04:00
|
|
|
namespace iq {
|
|
|
|
|
|
|
|
/* Information about a capture. */
|
|
|
|
struct CaptureInfo {
|
2023-09-21 11:43:10 -04:00
|
|
|
uint64_t file_size;
|
2023-09-23 15:56:37 -04:00
|
|
|
uint64_t sample_count;
|
2023-09-21 11:43:10 -04:00
|
|
|
uint8_t sample_size;
|
2023-09-23 15:56:37 -04:00
|
|
|
uint32_t max_power;
|
2023-10-19 14:14:25 -04:00
|
|
|
uint32_t max_iq;
|
2023-09-21 11:43:10 -04:00
|
|
|
};
|
|
|
|
|
2023-09-23 15:56:37 -04:00
|
|
|
/* Holds sample average power by bucket. */
|
2023-09-21 11:43:10 -04:00
|
|
|
struct PowerBuckets {
|
|
|
|
struct Bucket {
|
2023-09-23 15:56:37 -04:00
|
|
|
uint32_t power = 0;
|
|
|
|
uint8_t count = 0;
|
2023-09-21 11:43:10 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Bucket* p = nullptr;
|
2023-09-23 15:56:37 -04:00
|
|
|
const size_t size = 0;
|
2023-09-21 11:43:10 -04:00
|
|
|
|
2023-09-23 15:56:37 -04:00
|
|
|
/* Add the power to the bucket average at index. */
|
|
|
|
void add(size_t index, uint32_t power) {
|
2023-09-21 11:43:10 -04:00
|
|
|
if (index < size) {
|
2023-09-23 15:56:37 -04:00
|
|
|
auto& b = p[index];
|
|
|
|
auto avg = static_cast<uint64_t>(b.power) * b.count;
|
|
|
|
|
|
|
|
b.count++;
|
|
|
|
b.power = (power + avg) / b.count;
|
2023-09-21 11:43:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-09-23 15:56:37 -04:00
|
|
|
/* Data needed to trim a capture by sample range.
|
|
|
|
* end_sample is the sample *after* the last to keep. */
|
|
|
|
struct TrimRange {
|
|
|
|
uint64_t start_sample;
|
|
|
|
uint64_t end_sample;
|
|
|
|
uint8_t sample_size;
|
2023-09-21 11:43:10 -04:00
|
|
|
};
|
|
|
|
|
2023-09-23 15:56:37 -04:00
|
|
|
/* Collects capture file metadata and samples power buckets. */
|
|
|
|
Optional<CaptureInfo> profile_capture(
|
2023-09-21 11:43:10 -04:00
|
|
|
const std::filesystem::path& path,
|
2023-09-23 15:56:37 -04:00
|
|
|
PowerBuckets& buckets,
|
|
|
|
uint8_t samples_per_bucket = 10);
|
|
|
|
|
|
|
|
/* Computes the trimming range given profiling info.
|
|
|
|
* Cutoff percent is a number 1-100. */
|
|
|
|
TrimRange compute_trim_range(
|
|
|
|
CaptureInfo info,
|
|
|
|
const PowerBuckets& buckets,
|
|
|
|
uint8_t cutoff_percent);
|
|
|
|
|
2023-10-19 14:14:25 -04:00
|
|
|
/* Multiplies samples in an IQ buffer by amplification value */
|
|
|
|
void amplify_iq_buffer(
|
|
|
|
uint8_t* buffer,
|
|
|
|
uint32_t length,
|
|
|
|
uint32_t amplification,
|
|
|
|
uint8_t sample_size);
|
|
|
|
|
2023-09-23 15:56:37 -04:00
|
|
|
/* Trims the capture file with the specified range. */
|
|
|
|
bool trim_capture_with_range(
|
|
|
|
const std::filesystem::path& path,
|
|
|
|
TrimRange range,
|
2023-10-19 14:14:25 -04:00
|
|
|
const std::function<void(uint8_t)>& on_progress,
|
|
|
|
const uint32_t amplification);
|
2023-09-21 11:43:10 -04:00
|
|
|
|
2023-09-23 15:56:37 -04:00
|
|
|
} // namespace iq
|
2023-09-21 11:43:10 -04:00
|
|
|
|
2023-09-23 15:56:37 -04:00
|
|
|
#endif /*__IQ_TRIM_H__*/
|