function readReducedValues(idx, infoReducedSweep, numReducedSweeps, channel_handle, channelInfo)
%Funtion to read in samples for one channel of type reduced
%   idx                     channel index
%   infoReducedSweep        struct with information on indices of reduced
%                           data
%   numReducedSweeps        number of reduced data sweeps of one channel
%   no output -> reduced data of all sweeps gets stored to base

    reducedSampleTS = libstruct('DMDReader_REDUCEDSAMPLE_TIMESTAMP');
    reducedSampleTS_Ptr = libpointer('DMDReader_REDUCEDSAMPLE_TIMESTAMP', reducedSampleTS);
    numValidSamples = uint64(0);
    nextSample = uint64(0);
    reducedData = struct;
    temp = struct();
    temp.complete.time = [];
    temp.complete.min = [];
    temp.complete.max = [];
    temp.complete.avg = [];
    temp.complete.rms = [];
    
    for m = 0:numReducedSweeps-1
        reducedData(m+1).sweep = struct('min', [], 'max', [], 'avg', [], 'rms', []);
        reducedData(m+1).sweep.time = [];
    end

    for m = 0:numReducedSweeps-1
        
        firstSampleIndex = infoReducedSweep(idx+1).channels(m+1).reducedSweeps.firstSample;
        lastSampleIndex = infoReducedSweep(idx+1).channels(m+1).reducedSweeps.lastSample;
        numReducedSamples = uint64(lastSampleIndex - firstSampleIndex + 1);
        
        for k = 0:numReducedSamples-1
            
            [DMDReader_ErrorCode, channel_handle, data_temp, numValidSamples, nextSample] = calllib('dmdlib', 'DMDReader_GetSamplesWithTS_ReducedValue_Seconds', ...
                            channel_handle, k + firstSampleIndex, 1, reducedSampleTS_Ptr.Value, numValidSamples, nextSample);
                        
            if (DMDReader_ErrorCode ~= 0)
               close(f)
               error(['Error: ', num2str(DMDReader_ErrorCode), ' DMDReader_GetSamplesWithTS_ReducedValue_Seconds'])
            end
            [~,sys] = memory;
            if (sys.PhysicalMemory.Available < sys.PhysicalMemory.Total * 0.2)
               close(f)
               error('Error: Not enough RAM available.')
            end
            reducedData(m+1).sweep.time = [reducedData(m+1).sweep.time data_temp.timestamp];
            reducedData(m+1).sweep.min = [reducedData(m+1).sweep.min data_temp.value.min]; 
            reducedData(m+1).sweep.max = [reducedData(m+1).sweep.max data_temp.value.max];
            reducedData(m+1).sweep.avg = [reducedData(m+1).sweep.avg data_temp.value.avg];
            reducedData(m+1).sweep.rms = [reducedData(m+1).sweep.rms data_temp.value.rms];
            
        end % for k numReducedSamples
        
        if(isempty(reducedData(m+1).sweep.time) ~= 1)
            name = ['sweep', num2str(m+1)];
            temp.(name) = reducedData(m+1).sweep;
            temp.complete.time = horzcat(temp.complete.time, reducedData(m+1).sweep.time);
            temp.complete.min = horzcat(temp.complete.min, reducedData(m+1).sweep.min);
            temp.complete.max = horzcat(temp.complete.max, reducedData(m+1).sweep.max);
            temp.complete.avg = horzcat(temp.complete.avg, reducedData(m+1).sweep.avg);
            temp.complete.rms = horzcat(temp.complete.rms, reducedData(m+1).sweep.rms);
        end
    end % for m numReducedSweeps
    name = matlab.lang.makeValidName(channelInfo.name);
    assignin ('base',[name, '_Reduced'], temp);
    
end

